Action Controller 完成但 AJAX 成功回调未调用?

发布于 2024-12-09 21:30:40 字数 831 浏览 2 评论 0原文

我有一个脚本可以创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泪冰清 2024-12-16 21:30:40

AJAX 的要点在于它是异步的,这意味着您只能在成功回调中处理结果。 $.ajax 方法向服务器发起 AJAX 请求并立即返回。在此阶段,finished 的值仍然为 false,并且您将离开该函数。很久以后,当 AJAX 完成时,就会执行成功回调。只有在此回调中,您才能使用从服务器发送的结果。

使用 AJAX 时,您不应该以顺序和同步的方式组织您的 javascript 代码。

所以它应该是这样的:

$.ajax({
    url: '/actioncontroller/savenumbermatrix',
    type: 'POST',
    data: JSON.stringify(object),
    dataType: 'json',
    processData: false,
    contentType: 'application/json; charset=utf-8',
    success: function (data) {
        // TODO: only here you can use the results of an AJAX call
    }
});

另外,我建议您让控制器操作始终返回操作结果而不是字符串:

[HttpPost]
public ActionResult SaveNumberMatrix(NumberViewModel model) {
    return Json("Finished");
}

代码的另一个问题是您将 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:

$.ajax({
    url: '/actioncontroller/savenumbermatrix',
    type: 'POST',
    data: JSON.stringify(object),
    dataType: 'json',
    processData: false,
    contentType: 'application/json; charset=utf-8',
    success: function (data) {
        // TODO: only here you can use the results of an AJAX call
    }
});

Also I would recommend you to have your controller actions always return action results and not string:

[HttpPost]
public ActionResult SaveNumberMatrix(NumberViewModel model) {
    return Json("Finished");
}

Another issue with your code is that you are indicating dataType: 'json' as response and returning a string Finished from the server which is an invalid JSON.

孤凫 2024-12-16 21:30:40

您没有收到成功回调,因为您没有发送回有效的 json,您需要发送回 json,因为您的 dataType 是 json。因此它可能会抛出错误。

来自 jQuery 文档:

dataTypeString
Default: Intelligent Guess (xml, json, script, or html)
The type of data that you're expecting back from the server. 

在您的情况下,您指定 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:

dataTypeString
Default: Intelligent Guess (xml, json, script, or html)
The type of data that you're expecting back from the server. 

In your case you are specifying json but clearly not sending back json

谁把谁当真 2024-12-16 21:30:40

您的 ajax 请求似乎未到达服务器。请使用ajax错误函数。

error: function(a,b,c){alert(a+b+c)}

并找出问题所在

It's seems like your ajax request does not reach to the server. please use ajax error function.

error: function(a,b,c){alert(a+b+c)}

and find out the problem

仅此而已 2024-12-16 21:30:40

添加async参数为false(async: false,)则AJAX方法将被同步调用。

Add async parameter as false (async: false,) then the AJAX method will be called synchronously.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文