Action Controller 完成但 AJAX 成功回调未调用?
我有一个脚本可以创建 JSON 对象并将其发送到我的操作控制器。 ActionController 接收对象并知道如何将其模型绑定到 ViewModel。
动作控制器非常简单,如下所示:
[HttpPost]
public String SaveNumberMatrix(NumberViewModel model) {
return "Finished";
}
AJAX 函数:
function saveNumberMatrix(object, actioncontroller) {
var finished = false;
$.ajax({
url: actioncontroller,
type: 'POST',
data: JSON.stringify(object),
dataType: 'json',
processData: false,
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert(data);
finished = true;
},
});
alert(finished);
return finished;
}
我已经调试了动作控制器,并且在我进入之前 javascript 警报已完成(假)
"return "Finished";
永远不会调用成功回调 我哪里做错了?
I have a script that creates a JSON object and sends it to my Action Controller. The ActionController recieves the object and knows how to modelbind it to a ViewModel.
The action controller is very simple and looks like this:
[HttpPost]
public String SaveNumberMatrix(NumberViewModel model) {
return "Finished";
}
The AJAX function:
function saveNumberMatrix(object, actioncontroller) {
var finished = false;
$.ajax({
url: actioncontroller,
type: 'POST',
data: JSON.stringify(object),
dataType: 'json',
processData: false,
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert(data);
finished = true;
},
});
alert(finished);
return finished;
}
I have debugged the Action Controller, and the javascript alerts finished (false) before i step into
"return "Finished";
The sucess callback is never called
Where am i doing it wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
AJAX 的要点在于它是异步的,这意味着您只能在成功回调中处理结果。
$.ajax
方法向服务器发起 AJAX 请求并立即返回。在此阶段,finished 的值仍然为 false,并且您将离开该函数。很久以后,当 AJAX 完成时,就会执行成功回调。只有在此回调中,您才能使用从服务器发送的结果。使用 AJAX 时,您不应该以顺序和同步的方式组织您的 javascript 代码。
所以它应该是这样的:
另外,我建议您让控制器操作始终返回操作结果而不是字符串:
代码的另一个问题是您将
dataType: 'json'
指示为响应,并且从服务器返回一个无效的 JSON 字符串Finished
。The whole point of AJAX is that it is asynchronous meaning that you can process the results only inside the success callback. The
$.ajax
method starts an AJAX request to the server and it returns immediately. At this stage the value of finished is still false and you are leaving the function. Much later when the AJAX completes the success callback is executed. It is only inside this callback that you can use the results sent from the server.When using AJAX you should not organize your javascript code in a sequential and synchronous way.
So it should be like this:
Also I would recommend you to have your controller actions always return action results and not string:
Another issue with your code is that you are indicating
dataType: 'json'
as response and returning a stringFinished
from the server which is an invalid JSON.您没有收到成功回调,因为您没有发送回有效的 json,您需要发送回 json,因为您的 dataType 是 json。因此它可能会抛出错误。
来自 jQuery 文档:
在您的情况下,您指定
json
但显然没有发回json
You're not getting to the success callback because you're not sending valid json back, you need to send json back as your dataType is json. Hence it's probably throwing an error.
From the jQuery docs:
In your case you are specifying
json
but clearly not sending backjson
您的 ajax 请求似乎未到达服务器。请使用ajax错误函数。
并找出问题所在
It's seems like your ajax request does not reach to the server. please use ajax error function.
and find out the problem
添加async参数为false(async: false,)则AJAX方法将被同步调用。
Add async parameter as false (async: false,) then the AJAX method will be called synchronously.