简体   繁体   English

JQuery Mobile + Phonegap:$ .ajax调用不起作用

[英]JQuery Mobile + Phonegap : $.ajax calls not working

I was searching around for solution but failed all the way. 我一直在寻找解决方案,但一路都失败了。 The following codes are working fine under JQuery 1.4.4, JQuery Mobile 1.0a2 and PhoneGap 0.9. 以下代码在JQuery 1.4.4,JQuery Mobile 1.0a2和PhoneGap 0.9下运行良好。 However, when I transferred it to JQuery 1.7.1, JQuery Mobile 1.1.0 and PhoneGap 1.5; 但是,当我将它转移到JQuery 1.7.1,JQuery Mobile 1.1.0和PhoneGap 1.5时; it keeps on fall under error. 它继续在错误下跌。 I tracked the http call through Fiddler and realized the ajax does call to the URL but why it will fall under error instead of success? 我通过Fiddler跟踪了http调用,并意识到ajax确实调用了URL,但为什么它会出错而不是成功? Please help! 请帮忙!

$.ajax({
type: "GET",
cache: false,
url: updateServer+'update.xml',
dataType: "xml",
error: function(xhr, settings, exception){
    alert('The update server could not be contacted.');
},
success: function(xml){
    // success code     
    }
});

make sure that you can access the web service from the emulator itself and have allowed the application to access internet connection. 确保您可以从模拟器本身访问Web服务,并允许应用程序访问Internet连接。

to do this, from within the emulator, open the default browser and enter the URL. 要在模拟器中执行此操作,请打开默认浏览器并输入URL。 it should not give you a 404 or any exception. 它不应该给你404或任何例外。

I had this problem with Phonegap 1.5. 我有Phonegap 1.5的这个问题。 Downgrading to Phonegap 1.4.1 solved the problem. 降级到Phonegap 1.4.1解决了这个问题。 I was frustrated for days on end and couldn't make sense of the issue. 我连续几天感到沮丧,无法理解这个问题。

jQuery Mobile has a whole page in the documentation about implementing with PhoneGap. jQuery Mobile在关于使用PhoneGap实现的文档中有一整页。 Check it out here. 在这里查看。

http://jquerymobile.com/test/docs/pages/phonegap.html http://jquerymobile.com/test/docs/pages/phonegap.html

You have to set permissions to allow cross-domain ajax calls. 您必须设置权限以允许跨域ajax调用。

Also! 也! Remember to change your code in your html files if you are porting over from a web app. 如果要从Web应用程序移植,请记住更改html文件中的代码。 It is likely you made get calls to url "../api/handler.php" or something. 很可能你打电话给网址“../api/handler.php”或其他东西。 You need to make all those calls absolute for use in PhoneGap. 您需要将所有这些调用绝对用于PhoneGap。 "http://mydomain.com/api/handler.php" “http://mydomain.com/api/handler.php”

Ok, I figure the issue is actually the URL itself. 好吧,我认为问题实际上是URL本身。 The URL address is valid as it is accessible but it doesn't belong to the same domain. URL地址有效,因为它可以访问,但它不属于同一个域。 For example, my html file with the JQuery resides in http://www.yahoo.com/index.html but the URL which I am trying to call is http://www.google.com . 例如,我的带有JQuery的html文件位于http://www.yahoo.com/index.html,但我尝试调用的URL是http://www.google.com

Browser prevents making an ajax call from a page hosted on one domain to a page hosted on a different domain (same origin policy) due to security issue. 由于安全问题,浏览器阻止从托管在一个域上的页面到托管在不同域(同一源策略)上的页面进行ajax调用。 My solution here is to use a php file to retrieve the relevant data from another domain while the html (with JQuery) is calling the php file as follow: 我的解决方案是使用php文件从另一个域检索相关数据,而html(使用JQuery)调用php文件如下:

 $.ajax({ type: "GET", cache: false, url: 'getcontent.xml', dataType: "xml", error: function(xhr, settings, exception){ alert('The update server could not be contacted.'); }, success: function(xml){ // success code } }); 

Thank you for all the given helps! 感谢您提供的所有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM