我正在开发一个基于PhoneGap 的移动应用程序。我需要使用 AJAX,我正在使用 jQuery。
我尝试使用 CORS 从 PHP 上的 WS 获取 JSON,但收到错误,并且错误消息为空。我在网络浏览器中测试了代码并且它有效。但是,当我将它与 PhoneGap 一起使用时,它不起作用。我使用 Fecebook WS 在 PhoneGAp 中测试了我的代码,它可以工作。这是我测试过的组合:
测试1
来源:file:///C:/.../index.html(Web 浏览器)
Web服务:http://localhost/.../gettemployees.php
结果:有效
测试2
来源:PhoneGap
Web服务:http://localhost/.../gettemployees.php
结果:不起作用
测试3
来源:PhoneGap
网络服务:https://graph.facebook.com/OldemarshCr
结果:有效
这是代码:
$.getJSON(url, function (data) {console.log(data);})
.success(function() { console.log("second success"); })
.error(function(jqXHR, textStatus, errorThrown) {
console.log('******* '+"error: " + textStatus+' *******');
});
这是 JSON 响应:
{"items":[{"id":"10","firstName":"Kathleen","lastName":"Byrne","title":"Sales Representative","picture":"kathleen_byrne.jpg","reportCount":"0"},{"id":"9","firstName":"Gary","lastName":"Donovan","title":"Marketing","picture":"gary_donovan.jpg","reportCount":"0"},{"id":"7","firstName":"Paula","lastName":"Gates","title":"Software Architect","picture":"paula_gates.jpg","reportCount":"0"]}
谢谢
最佳答案
在ajax调用中,尝试添加
$.ajax({
crossDomain: true,
xhrFields: {withCredentials: true}
});
关于android - PhoneGap、jQuery getJSON 和 CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17160251/