【发布时间】:2012-01-19 10:28:01
【问题描述】:
我想开发一个 Phonegap 应用程序,我正在使用 jQuery Mobile。我在 PC 上通过 Firefox 进行开发和测试,所以这里描述的问题与 Phonegap 没有任何关系 - 这是一个 Firefox PC 问题:
以下代码不起作用,我需要一些帮助来为我指明正确的方向:
var loadWeather = function()
{
// Request absetzen
$.ajax(
{
// the URL for the request
url : 'http://www.google.com/ig/api',
// the data to send (will be converted to a query string)
data : {
weather : 'Vienna'
},
// whether this is a POST or GET request
type : 'GET',
// the type of data we expect back
dataType : 'xml',
// code to run if the request succeeds; the response is passed to the function
success : function(xml)
{
parseXML(xml);
},
// code to run if the request fails;
// the raw request and status codes are passed to the function
error : function(xhr, status)
{
alert('Error retreiving weather!');
}
});
}
状态是“错误”,xhr.readyState=0,xhr.status=0,所以我根本没有从 jQuery 那里得到任何信息。请求被执行,answer-header(来自 Firebug)是:
Accept: application/xml, text/xml, */*; q=0.01
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Encoding: gzip, deflate
Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Connection: keep-alive
Host: www.google.com
Origin: null
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
和 200 确定。那么为什么我要跳入错误案例呢? xhr.isRejected() 说的是真的。这是什么意思?
【问题讨论】:
-
在 XML 上尝试 JSONP
-
由于跨域来源限制,您将无法测试它(使用真实请求,没有一些代理脚本)。这个 API 似乎不支持 JSONP。但它可以在设备上的 Phonegap 内部工作,因为没有跨域限制。
-
@dfsg:如果您的评论是一个答案,我会选择它。谢谢!