Will jQuery .load() Work On PhoneGap?

2019-01-27 00:54发布

问题:

I know that the jQuery .load() function has a "problem": You can't retrieve pages that are outside of the current domain, because of the Same Origin Policy, but I remember when I was developing another program that I could do cross-domain AJAX without problems while on an PhoneGap compiled environment, but will it work while on PhoneGap(like normal AJAX) or it will just fail because of the policy?

回答1:

You can use .load() or $.ajax() in PhoneGap applications. Most of my experience is with getting information from the same domain under which the app. is packaged. For example:

App. package identifier:

com.my-domain.my-app

Website domain for ajax calls:

www.my-domain.com


I just did a simple test in an iPhone emulator (via Xcode) and I was able to get the contents of a personal web domain as well as http://www.google.com/. Test was as follows:

$(document).ready(function () {
    $.get('http://www.google.com/', function (data) {
        alert(data);
    });
});


回答2:

I tried doing this, testing on the ipad simulator and it didnt work for me, I kept getting cross-domain ajax errors (i.e. permission errors).

I eventually found that I needed to Navigate to [projectName] -> Support Files -> phonegap.plist Under External Hosts add a new string with the value * Or add your exact domain

(From this link thanks to Dror 'Yitzhakov).