jQuery AJAX 请求不返回(无 HTTP 状态代码)并保持挂起状态



我有几个这样的请求:

   $('#some-button').on('click', function(e) {
      e.preventDefault(); 
      $this = $(this);
      $.ajax({
         url: 'some_request.php/?q='+$some_id,
         dataType: 'json',
         success: function(data) {
            alert('success!')
         }          
      }); 
   });

即许多在单击按钮时启动的 AJAX 请求。 根据Chrome,此请求保持"待处理" - 没有JS警报。 没有返回 HTTP 响应代码。 我认为它只是坐在那里等待回应而没有得到回应。

问题类似于:jquery $.ajax 请求仍处于挂起状态,但答案无济于事。 在我的PHP或HTML代码中,我没有使用会话。

有什么想法吗? 非常感谢。

我认为你需要一个jQuery Ajax错误处理函数。在这里:

// jQuery Ajax Error Handling Function
$.ajaxSetup({
    error: function(jqXHR, exception) {
        if (jqXHR.status === 0) {
            alert('Not connect.n Verify Network.');
        } else if (jqXHR.status == 404) {
            alert('Requested page not found. [404]');
        } else if (jqXHR.status == 500) {
            alert('Internal Server Error [500].');
        } else if (exception === 'parsererror') {
            alert('Requested JSON parse failed.');
        } else if (exception === 'timeout') {
            alert('Time out error.');
        } else if (exception === 'abort') {
            alert('Ajax request aborted.');
        } else {
            alert('Uncaught Error.n' + jqXHR.responseText);
        }
    }
});

现在调用你的 ajax 函数:

$.ajax({
    url: 'some_request.php/?q=' + $some_id,
    dataType: 'json',
    success: function(data) {
        alert('success!')
    }
}); 

并检查您遇到的错误。

您确定您请求的 Php 文件位于同一域中吗?通常,在执行大多数 Web 浏览器不允许的跨域 Ajax 调用时会发生这种情况。如果是这种情况,您必须尝试 jsonp。这里有一个很好的实用解释和例子: http://www.jquery4u.com/json/jsonp-examples/

最新更新