jQuery ajax:error runs even if the response is OK 200

后端 未结 5 423
轮回少年
轮回少年 2021-01-17 15:21

I have a form which submit a form via AJAX with :remote => true. Looking at the server log and FireBug, I get the response 200 OK and it returns JSON in the form of:

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-17 15:58

    JSON.parse('') throws an error. To me, that is stupid, it should return undefined. I added this code to my app

    #HACK JSON.parse('') should return undefined, not throw an error
    _parse = JSON.parse
    JSON.parse = (str) =>
      unless str == ''
        _parse.apply JSON, arguments
    

    or for u poor people not using coffeescript ( untested )

    //HACK JSON.parse('') should return undefined, not throw an error
    var _parse = JSON.parse
    JSON.parse = function(str) {
      if (str !== '')
        return _parse.apply(JSON, arguments);
      else
        return undefined;
    }
    

提交回复
热议问题