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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9766425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:46:01  来源:igfitidea点击:

JQuery Mobile + Phonegap : $.ajax calls not working

javascriptjqueryajaxcordovamobile

提问by yi2ng2

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. However, when I transferred it to JQuery 1.7.1, JQuery Mobile 1.1.0 and 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? Please help!

我正在四处寻找解决方案,但一直失败。以下代码在 JQuery 1.4.4、JQuery Mobile 1.0a2 和 PhoneGap 0.9 下运行良好。但是,当我将其转移到 JQuery 1.7.1、JQuery Mobile 1.1.0 和 PhoneGap 1.5 时;它一直处于错误状态。我通过 Fiddler 跟踪了 http 调用并意识到 ajax 确实调用了 URL,但为什么它会出错而不是成功?请帮忙!

$.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     
    }
});

采纳答案by Martin Ongtangco

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. it should not give you a 404 or any exception.

为此,从模拟器中打开默认浏览器并输入 URL。它不应该给你一个 404 或任何异常。

回答by darryn.ten

I had this problem with Phonegap 1.5. Downgrading to Phonegap 1.4.1 solved the problem. I was frustrated for days on end and couldn't make sense of the issue.

我在 Phonegap 1.5 上遇到了这个问题。降级到 Phonegap 1.4.1 解决了这个问题。我连续几天感到沮丧,无法理解这个问题。

回答by Federico

jQuery Mobile has a whole page in the documentation about implementing with PhoneGap. Check it out here.

jQuery Mobile 在有关使用 PhoneGap 实现的文档中有一整页。在这里查看。

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. It is likely you made get calls to url "../api/handler.php" or something. You need to make all those calls absolute for use in PhoneGap. "http://mydomain.com/api/handler.php"

还!如果您从 Web 应用程序移植过来,请记住更改 html 文件中的代码。很可能您对 url "../api/handler.php" 或其他东西进行了 get 调用。您需要将所有这些调用设为绝对以在 PhoneGap 中使用。“http://mydomain.com/api/handler.php”

回答by yi2ng2

Ok, I figure the issue is actually the URL itself. The URL address is valid as it is accessible but it doesn't belong to the same domain. For example, my html file with the JQuery resides in http://www.yahoo.com/index.htmlbut the URL which I am trying to call is http://www.google.com.

好的,我认为问题实际上是 URL 本身。URL 地址是有效的,因为它是可访问的,但它不属于同一个域。例如,我的带有 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. 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:

由于安全问题,浏览器会阻止从一个域上托管的页面到另一个域上托管的页面(同源策略)进行 ajax 调用。我的解决方案是使用 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     
    }
});
$.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!

感谢您提供的所有帮助!