javascript - get json from controller MVC in java script - Stack Overflow

I am trying to make some mvc application and I have a problem. I want to get json from mvc controller i

I am trying to make some mvc application and I have a problem. I want to get json from mvc controller in javascript file:

JS:

var actionUrl = '@Url.Action("GetJsonTest", "JsonTest")';
$.getJSON(actionUrl, function (response) {
    if (response != null) {
        var html = "<li>" + response.Id + " " + response.Name + " " + response.Author + "</li>";
        $("h2").append(html);
    }
});

Controller:

[HttpGet]
public ActionResult GetJsonTest() {
    var books = new Book() {
        Id = '1',
        Name = "1+1",
        Author = "Varnava",
        Price = 100
    };
    return Json(books, JsonRequestBehavior.AllowGet);
}

It's not working. I have 404 error. It cant get something from controller. What am I doing wrong? Sorry for bad English. Hope you understand what I ask.

I am trying to make some mvc application and I have a problem. I want to get json from mvc controller in javascript file:

JS:

var actionUrl = '@Url.Action("GetJsonTest", "JsonTest")';
$.getJSON(actionUrl, function (response) {
    if (response != null) {
        var html = "<li>" + response.Id + " " + response.Name + " " + response.Author + "</li>";
        $("h2").append(html);
    }
});

Controller:

[HttpGet]
public ActionResult GetJsonTest() {
    var books = new Book() {
        Id = '1',
        Name = "1+1",
        Author = "Varnava",
        Price = 100
    };
    return Json(books, JsonRequestBehavior.AllowGet);
}

It's not working. I have 404 error. It cant get something from controller. What am I doing wrong? Sorry for bad English. Hope you understand what I ask.

Share Improve this question edited Mar 14, 2017 at 21:02 Alex 721 silver badge10 bronze badges asked Mar 14, 2017 at 20:30 Stanislav MuryndinStanislav Muryndin 973 silver badges10 bronze badges 5
  • 1 What's the name of the Controller? From you Url.Action, I would assume JsonTest, but is it actually called JsonTestController.cs? – sleeyuen Commented Mar 14, 2017 at 20:39
  • Your code should work as long as your GetJsonTest method is inside JsonTestController.cs – Shyju Commented Mar 14, 2017 at 20:42
  • @sleeyuen hi, yes, it is actually called JsonTestController. – Stanislav Muryndin Commented Mar 14, 2017 at 20:51
  • 1 cshtml files will be intepreted by the MVC template server. Anything else will not so your js file is tryin to make the literal request to url '@Url.Action("GetJsonTest", "JsonTest")'. If you did some debugging in your browser this would be immediately obvious. – Igor Commented Mar 14, 2017 at 21:06
  • @csharpbd gotcha. i've got it. Thank you! – Stanislav Muryndin Commented Mar 14, 2017 at 21:19
Add a ment  | 

2 Answers 2

Reset to default 3

You have mentioned that you are trying to call GetJsonTest from a javascript file: js. You have used to generate ajax url using

var actionUrl = '@Url.Action("GetJsonTest", "JsonTest")';

above Razor statement from .js file. The problem is that the javascript file can't execute Razor statement. But in .cshtml can. So, if you use this Razor statement from javascript within .cshtml it will work and if you use this Razor statement from javascript within .js it will not work. You have to call like this from .js file:

var actionUrl = '/JsonTest/GetJsonTest';
$.getJSON(actionUrl, function (response) {
    if (response != null) {
        $("h2").append("<li>" + response.Id + " " + response.Name + " " + response.Author + "</li>")
    }
});

have you defined route in your routeconfig.cs?

you should have something like

   public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
                routes.MapRoute(
                name: "YOUR NAME",
                url: "{YOUR CONTROLLER NAME}/{GetJsonTest}",
                defaults: new { controller = "YOUR CONTROLLER NAME", action = "GetJsonTest" }
            );
    }
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745527577a4631542.html

相关推荐

  • javascript - get json from controller MVC in java script - Stack Overflow

    I am trying to make some mvc application and I have a problem. I want to get json from mvc controller i

    13小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信