简体   繁体   English

Ajax 请求随机失败

[英]Ajax Request Randomly Fails

whats hapenning is that my ajax request randomly fails, and I don't know why.发生了什么是我的ajax请求随机失败,我不知道为什么。

I've been testing only in chrome and when the error callback gets called the controller returns successfully so i think that the problem is not in the server side.but i'm not sure.我一直只在 chrome 中进行测试,当错误回调被调用时,控制器成功返回,所以我认为问题不在服务器端。但我不确定。 the responseText error in chrome is blank so i have no tip to troubleshoot. chrome 中的 responseText 错误是空白的,所以我没有解决问题的技巧。

This is My Ajax call m I doing somehting wrong, I'm Clueless?这是我的 Ajax 调用,我做错了什么,我一无所知?

$.ajax({
    type: "GET",
    url: url,
    data: { postalCode: postalCode },
    dataType: "json",
    success: function (response) {
        if (isPostBack != 'True') {
            switch (response["Code"]) {
                case "-1":
                    alert('msg 1.');
                    break;
                case "0":
                    alert('msg 2.');
                    break;
                case "1":
                    alert('msg 3.');
                    break;
                case "2":
                    alert('msg 4.');
                    break;
                default:
                    alert('unexpected value.');
            }
        }
    }
});

if not what could be the most likely causes?如果不是,最可能的原因是什么? I'm Developing Asp.NET MVC for Sitefinity, and I only detect this issue in this ajax request.我正在为 Sitefinity 开发 Asp.NET MVC,我只在这个 ajax 请求中检测到这个问题。

UPDATE:更新:

I've detected in the browser that the request is being cancelled.我在浏览器中检测到请求被取消。 it arrives successfully to the server and is cancelled during the code execution.它成功到达服务器并在代码执行期间被取消。 it is not cancelled in a specific line because I commented the lines to find which one is causing troubles but it was cancelled regardless of the code line.它没有在特定行中取消,因为我对这些行进行了注释,以找出哪一行导致了问题,但无论代码行如何,它都被取消了。 Then I started thinking about timeout and added a timeout.然后我开始考虑超时并添加了超时。 first 3 seconds than 10 seconds.前 3 秒比 10 秒多。 but the problem was still there.但问题仍然存在。 this is the request status:这是请求状态:

在此处输入图片说明

Suggesting a slight modification:建议稍作修改:

$.getJSON(url, {"postalCode": postalCode})
 .success(function (response) {
    if (isPostBack != 'True') {
        switch (response.Code) {
        case "-1":
            alert('msg 1.');
            break;
        case "0":
            alert('msg 2.');
            break;
        case "1":
            alert('msg 3.');
            break;
        case "2":
            alert('msg 4.');
            break;
        default:
            alert('unexpected value.');
        }
    }
});

The Problem问题

I had the ajax request been made from a submit button, which cancelled some of the requests.我从提交按钮发出了 ajax 请求,这取消了一些请求。

I solve it by我解决了

  • hiding the submit button隐藏提交按钮
  • creating a button type=button that does the ajax call创建一个按钮 type=button 执行 ajax 调用
  • on success callback i click the hidden button to submit the form values $("#btnsubmit").click();在成功回调我点击隐藏按钮提交表单值 $("#btnsubmit").click();

After this I still had a problem, the first request was always cancelled.在此之后我仍然有问题,第一个请求总是被取消。 The problem was timeout.问题是超时。 when I increased the timeout value to 20 seconds it stops from being cancelled.当我将超时值增加到 20 秒时,它停止被取消。 but leads me to another problem.但让我想到了另一个问题。

why it needed 20 seconds in the first ajax request?为什么在第一个 ajax 请求中需要 20 秒?

Well that will be another Adventure..那么这将是另一个冒险..

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

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