最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ajax call not reaching controller method - Stack Overflow

programmeradmin0浏览0评论

Yes, there are a whole bunch of posts with similar questions. I've tried to follow the answers in them, but my ajax call still isn't reaching the controller method.

controller (SalesController.cs):

[HttpGet]
public JsonResult fillVarietiesSelect(int species_id)
{
    String[] alist = {"a","b","c"};
    return Json(alist, JsonRequestBehavior.AllowGet);
}

javascript:

$('#species-select').on('change', function () {
    var species_id = $('#species-select').val();
    console.log("species id selected " + species_id);
    alert("species id selected " + species_id);
    $('#variety-select').empty();
    $.ajax({
        type: 'GET',
        url: '@Url.Action("fillVarietiesSelect", "Sales")',
        data: {species_id : species_id},
        success: function (result) {
            alert(result);
        }
    });
});

The on change event is firing, and the alert pops up with the correct data. I have a breakpoint set at the controller method, but execution doesn't seem to get there.

Yes, there are a whole bunch of posts with similar questions. I've tried to follow the answers in them, but my ajax call still isn't reaching the controller method.

controller (SalesController.cs):

[HttpGet]
public JsonResult fillVarietiesSelect(int species_id)
{
    String[] alist = {"a","b","c"};
    return Json(alist, JsonRequestBehavior.AllowGet);
}

javascript:

$('#species-select').on('change', function () {
    var species_id = $('#species-select').val();
    console.log("species id selected " + species_id);
    alert("species id selected " + species_id);
    $('#variety-select').empty();
    $.ajax({
        type: 'GET',
        url: '@Url.Action("fillVarietiesSelect", "Sales")',
        data: {species_id : species_id},
        success: function (result) {
            alert(result);
        }
    });
});

The on change event is firing, and the alert pops up with the correct data. I have a breakpoint set at the controller method, but execution doesn't seem to get there.

Share Improve this question asked May 26, 2015 at 2:12 abalterabalter 10.4k18 gold badges98 silver badges167 bronze badges 7
  • Do you see any error in the console or request network tab? – Shaunak D Commented May 26, 2015 at 2:23
  • ASP.NET MVC throws so many console errors! But the last is: Empty string passed to getElementById(). jquery-1.10.2.js:188:0 GET XHR http://localhost:65244/@Url.Action(%22fillVarietiesSelect%22,%20%22Sales%22) – abalter Commented May 26, 2015 at 3:01
  • The network panel is showing a 404 error for GET @Url.Action("fillVarietiesSelect", "Sales") – abalter Commented May 26, 2015 at 3:04
  • If you make you controller action method return string then it works file. The problem is ing when you return json. try returning list<string> instead of json – user786 Commented May 26, 2015 at 3:34
  • Try changing HttpGet to HttpPost in Controller method. Also, change ajax type from type: 'GET' to type:'POST'. – JGV Commented May 26, 2015 at 3:44
 |  Show 2 more ments

3 Answers 3

Reset to default 1

try doing exactly like following

Controller:

    [HttpGet]
    public ActionResult receive2(string p)
    {
        ViewBag.name = p;
        List<string> lst = new List<string>() { p };
        return Json(lst,JsonRequestBehavior.AllowGet);

    }

Client side

      $.ajax({
            type: "GET",
            url: "Main/receive2", // the method we are calling
            contentType: "application/json; charset=utf-8",
            data: { "p": $("#txtname").val() },
             dataType:"json",
            success: function (result) {
                alert("yes");
                alert('Yay! It worked!tim' + result);
                window.location = "http://google.";
                // Or if you are returning something

            },
            error: function (result) {
                alert('Oh no aa :(' + result[0]);
            }

        });

I have checked it's working

I had the same issue, changing the parameter from int to string resolved the problem.

On the server side I had,

public string RemoveByDocumentID(int DocumentID)

changing that to

public string RemoveByDocumentID(string DocumentID)

resolved the issue.

On the same context, I have another method in the same name (overloaded method). Which was also contributing to the issue, so I made the method name unique.

Your 404 error is telling you that @Url.Action is not being parsed by the view engine. The @ is Razor syntax, so for this to work you need to be using a .cshtml extension for C# views or .vbhtml for VB views and be using MVC 3 or above.

发布评论

评论列表(0)

  1. 暂无评论