weixin_33696106 2013-05-23 15:12 采纳率: 0%
浏览 14

jQuery Ajax未完成

I'm having a trouble with ajax requests and server responses:

$.ajax({
    url: servurl,
    dataType: "jsonp",
    data: {... },
    crossDomain: true,
    error: function(){},
    success: function(){},
    complete: function(){alert('complete')}
});
}

The thing is - sometimes I get succes, when I should get it, but sometimes I can get 500 status, and it is normal and expected. The same ajax call works for correct requests, but fails for others. I want to display an error message if I get a 500 server error, but for some reason the ajax does not complete. Thus, neither error: nor complete: work. Maybe the reason for that is 'jsonp' datatype? Other datatypes do not work though. Can someone help please?

Or maybe give me an advice on how to detect server status any other way.

  • 写回答

4条回答 默认 最新

  • weixin_33725272 2013-05-23 15:19
    关注

    Make sure that you are accessing to your server. Maybe you are requesting in your server for an specific contentType (like application/json) and you are not using that property into your ajax call.

    As you requested, to show any message if get a error (400, 404, 500...), you can use my custom function for ajax error responses:

    function onErrorFunc(jqXHR, status, errorText) {
        alert('Status code: ' + jqXHR.status + '
    Status text: ' + status +
        '
    Error thrown: ' + errorText);
    }
    

    Usage:

    $.ajax({
        //some options
        error: onErrorFunc
    });
    

    Please, show us what error thrown your server.

    评论

报告相同问题?

悬赏问题

  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))