使用cordova调用错误的ajax回调



在我的jqm应用程序中,我使用jQuery $制作POST。Ajax发送和接收json数据。在浏览器和iPhone上一切正常;在Android上,我注意到当服务器响应如下:

{ "code" : 500,
  "errorMsg" : "bla bla bla",
  "errors" : null,
  "status" : "INTERNAL_SERVER_ERROR",
  "success" : false
}

ajax调用"error"回调,而不是"success"。这只发生在android上,并且只在我包含corova-2.0.0时发生。Js在这个项目上。任何帮助吗?

我正在使用cordova-2.0.0与jqm 1.3.1和jQuery 1.9.1

下面是我的代码:

 var ajax = $.ajax({
   type: "post",
   url: url,
   dataType: 'json',
   contentType: 'application/json; charset=utf-8',
   data: data,
   timeout: 30000
});
var success = function (d) {
   if(d.success==true && obj.success)
       obj.success(d.data);
   else
   {
       var msg = parseErrors(d);
       console.log(msg);
       //open page passing results
       obj.msgBus.fire("RequestResult", {
           callback: function(){  obj.msgBus.fire("Welcome");},
           success: false,
           text: msg
       });
   }
};
var error = function (xhr, status, e) {
   console.log('error ajax url:[' + url + ']  status:[' + status + ']  error:[' + e + ']');
   if (obj.error) {
       if ((typeof e == 'string'))
           obj.error({
               statusText: e,
               code: xhr.statusCode,
               text: xhr.responseText
           });
       else {
           e = $.extend(e, {
               statusText: status
           });
           obj.error(e);
       }
   }
};
var complete = function () {
   if (obj.complete) {
       obj.complete();
   }
};
var parseErrors = function(d){
   console.log( d);
    if(d.errors==null){
        return d.errorMsg;
   }
   else
   {
       var res="";
       for (var i=0;i< d.errors.length;i++){
            res+= "{0}: {1} <br/>".format( d.errors[i].field, d.errors[i].errorMsg);
       }
       return res;
   }
};
ajax.success(success).error(error).complete(complete);

,其中data是一个像这样的对象:

{ 
   "date" : 1371679200000,
  "idBeach" : "1",
  "idStuff" : 3,
  "idUser" : "8",
  "numStuff" : 1
}

试试这个,它对我有用

$.ajax({
    async: false,
    type: "POST",
    url: "Your_URL",
    dataType: "json",
    success: function (data, textStatus, jqXHR) {

        $.each(data, function (i, object) {
            alert(obj.Data);
        });
    },
    error: function () {
        alert("There was an error loading the feed");
    }
});

最新更新