简体   繁体   English

从jQuery Ajax的错误功能的控制器中获取数据— Asp.net MVC

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

I have a jquery ajax script like following 我有一个jQuery的ajax脚本如下

    $.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. 我正在调用Controller操作方法。 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. 但是我收到的数据在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. 发生错误的原因是,您已指定返回的数据类型为json(在dataType: "json", ),但是您的方法返回了文本。 You have 2 options. 您有2个选择。

  1. Change the controller method to return json using return Json(p); 更改控制器方法以使用return Json(p);返回json return Json(p);
  2. Change the ajax option to dataType: "text", or just omit it 将ajax选项更改为dataType: "text",或将其忽略

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 ) 注意:您应该始终使用@Url.Action()生成正确的url,在这种情况下,不必对数据进行字符串化(但是您需要删除contentType:行,因此它使用了默认的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). 此外,严格来说,这不是POST(您不更改服务器上的数据-但我认为这只是为了测试)。 And there is no point in the line ViewBag.name = p; ViewBag.name = p;行中没有任何意义ViewBag.name = p; - it does nothing in your context, and as soon as you return from the method, ViewBag is lost anyway. -它在您的上下文中没有任何作用,一旦您从方法中返回, ViewBag就会丢失。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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