简体   繁体   中英

getting data from controller in error function of jquery ajax — Asp.net MVC

I have a jquery ajax script like following

    $.ajax({
            type: "POST",
            url: "Main/receive", // the method we are calling
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({ 'p':$("#txtname").val() }),
            dataType: "json",
            success: function (result) {
                alert('Yay! It worked!');
                // Or if you are returning something

            },
            error: function (result) {
                alert('Oh no zzzz:('+result.responseText);
            }
        });

And I am calling to Controller action method. The data is sending to the controller's action method and I am also receiving data from the controller. But the data that I am receiving is inside the error function of jquery ajax.

I want it to be inside the success function.

Why my success function is not getting called.

Following is my controller's action method,

   [HttpPost]
    public string receive(string p)
    {
        ViewBag.name = p;
        return p;

    }

The reason for the error is that you have specified that the returned data type be json (in the line dataType: "json", ) but you method returns text. You have 2 options.

  1. Change the controller method to return json using return Json(p);
  2. Change the ajax option to dataType: "text", or just omit it

However you can improve your script as noted below

$.ajax({
  type: "POST",
  url: '@Url.Action("receive", "Main")', // don't hardcode url's
  data: { p: $("#txtname").val() }, // no need to stringify (delete the contentType: option)
  dataType: "json",
  success: function (result) {
      alert('Yay! It worked!');
  },
  error: function (result) {
      alert('Oh no zzzz:('+result.responseText);
  }
});

or even simpler

$.post('@Url.Action("receive", "Main")', { p: $("#txtname").val() }, function(result) {
    alert('Yay! It worked!');
}).fail(function(result) {
    alert('Oh no zzzz:('+result.responseText);
});

Notes: You should always use @Url.Action() to generate the correct urls, and it is not necessary in this case to stringify the data (but you need to remove the contentType: line so it used the default application/x-www-form-urlencoded; charset=UTF-8 )

In addition, this is not strictly a POST (your not changing data on the server - but I assume this is just for testing). And there is no point in the line ViewBag.name = p; - it does nothing in your context, and as soon as you return from the method, ViewBag is lost anyway.

try to change your controller code as follows

[HttpPost]
 public ActionResult List(string p)
    {
       ViewBag.name = p;
       return Json(ViewBag);
    }

Your controller method should look like this:

[HttpPost]
public ActionResult receive(string p)
{
   return Json(p);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM