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

jQuery Ajax未完成

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

    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.

图片转代码服务由CSDN问答提供 功能建议



     dataType:“ jsonp”,


问题是-有时我应该获得成功,但有时我可以获得500状态,这是正常且预期的。 相同的ajax调用适用于正确的请求,但对于其他请求则失败。 如果遇到500服务器错误,我想显示一条错误消息,但是由于某种原因,ajax无法完成。 因此, error: complete:均无效。 也许原因是“ jsonp”数据类型? 但是其他数据类型不起作用。 有人可以帮忙吗?


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • weixin_33725272
    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);


        //some options
        error: onErrorFunc

    Please, show us what error thrown your server.

    点赞 评论
  • weixin_33713503
    weixin_33713503 2013-05-23 15:25

    jsonp requests do not trigger error callbacks by design, therefore there is no way for you to catch the error with javascript. I suggest instead implementing an error handler on your server that detects a jsonp request and returns jsonp that indicates an error has occured rather than a 500 status code.

    点赞 评论
  • weixin_33692284
    weixin_33692284 2013-05-23 15:55

    Note that error: is deprecated as of 1.8 and is not called for JSONP however I wonder if you might have success using the Promise functionality introduced with 1.5 for deferred http://api.jquery.com/category/deferred-object/ as:

    jqXHR.fail(function(jqXHR, textStatus, errorThrown) {});
    jqXHR.done(function(data, textStatus, jqXHR) {});
    jqXHR.always(function(data|jqXHR, textStatus, jqXHR|errorThrown) { });

    Example for your code:

        url: servurl,
        dataType: "jsonp",
        data: {... },
        crossDomain: true
    }).done(function(data, textStatus, jqXHR){ //replace success
    }).always(function(data|jqXHR, textStatus, jqXHR|errorThrown) { // replace complete
    }).fail(function(jqXHR, textStatus, errorThrown) { // replace error
    点赞 评论
  • weixin_33688840
    weixin_33688840 2013-05-24 07:41

    Thank you all for comments. Jquery .ajax really does not give errors on jsonp requests. The way to get error messages was to implement the jquery-jsonp plugin: https://github.com/jaubourg/jquery-jsonp

    点赞 评论