从控制器获取数据的jQuery AJAX的误差函数 - Asp.net MVC误差、控制器、函数、数据

2023-09-06 10:27:39 作者:和好算了

我有一个像下面的一个jquery AJAX脚本

  $。阿贾克斯({
            键入:POST,
            网址:主/接收,//我们调用的方法
            的contentType:应用/ JSON的;字符集= UTF-8,
            数据:JSON.stringify({'P'。$(#改为txtName)VAL()}),
            数据类型:JSON,
            成功:函数(结果){
                警报('!耶成功了!');
                //或者,如果你正在返回的东西

            },
            错误:函数(结果){
                警报('哦,不ZZZZ:('+ result.responseText);
            }
        });
 

和我打电话给控制器的操作方法。该数据被发送到控制器的操作方法,我也从控制器接收数据。但是,我收到的数据是jQuery的AJAX的误差函数里面。

我希望它是成功的函数中。

为什么我的成功,函数不获取调用。

以下是我的控制器的操作方法,

  [HttpPost]
    公共字符串接收(串P)
    {
        ViewBag.name = P;
        返回磷;

    }
 

解决方案

的原因的错误是,你必须指定返回的数据类型是JSON(在该行数据类型:JSON, ),但你方法返回文本。你有两个选择。

改变控制器方法使用返回JSON 返回JSON(P); 更改AJAX选项数据类型:文字,或只是忽略它

不过,你可以提高你的脚本如下所述

  $。阿贾克斯({
  键入:POST,
  网址:@ Url.Action(接收,主)',//不要硬code网址
  数据:{号码:$(#改为txtName)VAL()} //无需字符串化(删除的contentType:选项)
  数据类型:JSON,
  成功:函数(结果){
      警报('!耶成功了!');
  },
  错误:函数(结果){
      警报('哦,不ZZZZ:('+ result.responseText);
  }
});
 
jQuery Ajax

或者也可以简单

  $后期('@ Url.Action(接收,主)',{号码:$(#改为txtName)VAL()},功能(结果) {
    警报('!耶成功了!');
}),失败(函数(结果){
    警报('哦,不ZZZZ:('+ result.responseText);
});
 

注:应始终使用 @ Url.Action()生成正确的网址,而不必在这种情况下字符串化数据(但你需要删除的contentType:行,因此使用了默认的应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8

另外,这不是严格意义上的POST(在服务器上没有变化的数据 - 但我认为这只是用于测试)。并且在该行没有一点 ViewBag.name = P; - 它确实没有在你的背景下,作为一旦你从方法返回, ViewBag 无论如何都会丢失。

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.

Change the controller method to return json using return Json(p); 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.