jQuery AJAX请求获得响应但没有触发回调

时间:2022-10-08 23:25:46

I have code as such:

我有这样的代码:

$.ajax({
     method: "GET",
     url: page,
     dataType: 'html',
     data:data,
     success: function(data){
        console.log(data);
     },
     error: function(){
        console.log('error');
     }
 });

Using either Chrome or Firefox's debugger I can see that the request is successful with a response of 200 OK and the response contains the expected data. My problem is, however, that no callbacks fire whatsoever. "Success" does not fire nor does "Error". Furthermore, no deferred methods fire either, such as "done", "then", or "always".

使用Chrome或Firefox的调试器,我可以看到请求成功,响应为200 OK,响应包含预期数据。但问题是,没有任何回调会引发任何回调。 “成功”不会触发,也不会出现“错误”。此外,也没有延迟方法触发,例如“完成”,“然后”或“始终”。

I've been trying to trouble shoot this for the past few hours to no avail. I'm at a total loss. I've also tried using methods such as "$.get" with the same result.

过去几个小时我一直试图解决这个问题无济于事。我完全失去了。我也尝试过使用像“$ .get”这样的方法,结果相同。

In short, I'm getting the response, but the code in jQuery is not firing any callbacks and all without any visible errors in the console.

简而言之,我得到了响应,但是jQuery中的代码没有触发任何回调,并且在控制台中没有任何可见的错误。

2 个解决方案

#1


1  

One thing I see wrong in your code is that:

我在你的代码中看到错误的一件事是:

method: "GET",

should be:

type: "GET",

I don't see any documented property for method in the jQuery doc. The type property is supposed to default to "GET" so this may not be the only thing wrong here.

我没有在jQuery doc中看到任何记录的方法属性。 type属性应该默认为“GET”,所以这可能不是唯一的错误。


In addition, there are cases where the error callback will not be called even if the ajax call fails (in cross-domain requests). From the jQuery doc for the error callback:

此外,即使ajax调用失败(在跨域请求中),也存在不会调用错误回调的情况。从jQuery doc for error callback:

This handler is not called for cross-domain script and cross-domain JSONP requests.

不为跨域脚本和跨域JSONP请求调用此处理程序。

This is because jQuery is expecting the server to send back a particular form of javascript and if the server doesn't do what is expected, then jQuery never knows when the request comes back and can't process it.

这是因为jQuery期望服务器发送回特定形式的javascript,如果服务器没有按预期执行,那么jQuery永远不知道请求何时返回并且无法处理它。

In these cases, you often have to figure out what might be going wrong from looking at the network trace in the debugger.

在这些情况下,您经常需要弄清楚在调试器中查看网络跟踪时可能出现的问题。


Other things to check to make sure you aren't accidentally cross domain:

要检查的其他事项,以确保您不会意外跨域:

  • Make sure the domain/subdomain are exactly the same between ajax call and the page. For example, one common mistake is for one to have www. on it and the other not.
  • 确保ajax调用和页面之间的域/子域完全相同。例如,一个常见的错误是一个人有www。在它上面而另一个没有。

  • Make both page and ajax URL are both same http or https.
  • 使页面和ajax URL都是相同的http或https。

  • If using a non-standard port number, make sure both page and ajax URL are using the same port.
  • 如果使用非标准端口号,请确保page和ajax URL使用相同的端口。

#2


0  

The following code works. Also note that AJAX will not work with cross site scripting. If you want to get the error you can print the "errorThrown"

以下代码有效。另请注意,AJAX不适用于跨站点脚本。如果您想获得错误,可以打印“errorThrown”

<script>
    $(document).ready(function () {
        $('#getLink').on("click", function () {
            var url = $("#myUrl");
            alert(url.val());
            $.ajax({

                type: "GET",
                url: url.val(),
                dataType: 'html',
                success: function (data, textStatus, xhr) {
                    $("#data").text(textStatus);
                },
                error: function (data,textStatus, errorThrown){
                    $("#data").text(errorThrown);
                }
            });
        });

    });

</script>

<input id="myUrl" name="myURL" type="text" value="http://localhost:35460/Home/TestPage.cshtml" />
<input type="button" value="getLink" id="getLink">
<span id="data"></span>

#1


1  

One thing I see wrong in your code is that:

我在你的代码中看到错误的一件事是:

method: "GET",

should be:

type: "GET",

I don't see any documented property for method in the jQuery doc. The type property is supposed to default to "GET" so this may not be the only thing wrong here.

我没有在jQuery doc中看到任何记录的方法属性。 type属性应该默认为“GET”,所以这可能不是唯一的错误。


In addition, there are cases where the error callback will not be called even if the ajax call fails (in cross-domain requests). From the jQuery doc for the error callback:

此外,即使ajax调用失败(在跨域请求中),也存在不会调用错误回调的情况。从jQuery doc for error callback:

This handler is not called for cross-domain script and cross-domain JSONP requests.

不为跨域脚本和跨域JSONP请求调用此处理程序。

This is because jQuery is expecting the server to send back a particular form of javascript and if the server doesn't do what is expected, then jQuery never knows when the request comes back and can't process it.

这是因为jQuery期望服务器发送回特定形式的javascript,如果服务器没有按预期执行,那么jQuery永远不知道请求何时返回并且无法处理它。

In these cases, you often have to figure out what might be going wrong from looking at the network trace in the debugger.

在这些情况下,您经常需要弄清楚在调试器中查看网络跟踪时可能出现的问题。


Other things to check to make sure you aren't accidentally cross domain:

要检查的其他事项,以确保您不会意外跨域:

  • Make sure the domain/subdomain are exactly the same between ajax call and the page. For example, one common mistake is for one to have www. on it and the other not.
  • 确保ajax调用和页面之间的域/子域完全相同。例如,一个常见的错误是一个人有www。在它上面而另一个没有。

  • Make both page and ajax URL are both same http or https.
  • 使页面和ajax URL都是相同的http或https。

  • If using a non-standard port number, make sure both page and ajax URL are using the same port.
  • 如果使用非标准端口号,请确保page和ajax URL使用相同的端口。

#2


0  

The following code works. Also note that AJAX will not work with cross site scripting. If you want to get the error you can print the "errorThrown"

以下代码有效。另请注意,AJAX不适用于跨站点脚本。如果您想获得错误,可以打印“errorThrown”

<script>
    $(document).ready(function () {
        $('#getLink').on("click", function () {
            var url = $("#myUrl");
            alert(url.val());
            $.ajax({

                type: "GET",
                url: url.val(),
                dataType: 'html',
                success: function (data, textStatus, xhr) {
                    $("#data").text(textStatus);
                },
                error: function (data,textStatus, errorThrown){
                    $("#data").text(errorThrown);
                }
            });
        });

    });

</script>

<input id="myUrl" name="myURL" type="text" value="http://localhost:35460/Home/TestPage.cshtml" />
<input type="button" value="getLink" id="getLink">
<span id="data"></span>