Phonegap for Windows Phone jQuery ajax callback no

2019-01-27 04:19发布

问题:

I have a problem with Phonegap for Windows Phone and jQuery.

I have an Ajax request to a distant server, but the success callback is not executed even if the server respond with a 200 code (I can see the request in the server access_log). The error callback is not executed either.

The code

$.support.cors = true;
$.mobile.allowCrossDomainPages = true;

$.ajax({
    url: 'http://example.com',
    data: { foo: 'bar' },
    cache: false,
    dataType: 'json',
    success: function (data) { 
        console.log(data); 
    }
});

Any idea ?

回答1:

It seems to be a known issue of local XHR shim implementation in phone gap 1.3.0. You can switch to previous version or comment everything in phonegap-1.3.0.js starting from line 3551 to the end of the file

//(function(win,doc){
//
//    doc.addEventListener("DOMContentLoaded",function()
//    { 
// ......
//      if(!docDomain || docDomain.length == 0)
//      {
//          //console.log("adding our own Local XHR shim ");
//          var aliasXHR = win.XMLHttpRequest;
//      
//          win.XMLHttpRequest = function(){};
//      
//          var UNSENT = 0;
// ......
//
//    
//})(window,document);

EDIT PhoneGap 1.4.0rc1 is commited

https://github.com/purplecabbage/callback-windows-phone

Changes include fixes for XHR to local files, + fixes to allow jQM single/multipage apps function correctly.



回答2:

I have the same issue, but not this advice, neither the new version of phonegap didn't help. With phonegap 1.4.0 I now get error callback always.



回答3:

If you're using jQuery Mobile, you need to set the following to true:

$.support.cors and $.mobile.allowCrossDomainPages

jQuery expects all js code to be run in a normal browser environment, where cross-domain requests are not allowed. I think they've come up with this especially for PhoneGap.

Check the jQuery Mobile Docs