我一直无法成功弄清楚为什么 $.ajax 拒绝在 Phonegap 中连接到互联网。相同的代码在标准 HTML 文件中运行良好。访问源设置为 *。
最初我以为 Phonegap 拒绝连接到互联网。但是,它远程加载 JQuery 和 JQuery Mobile(通过链接标记)。 JQuery 继续返回错误。在 JBoss Developer Studio (PG 4.1.2) 和 PG 命令行 4.3 中进行了测试。
字符串化错误对象返回的错误
error: {"readyStaate":4,"responseText":"{\n \"code\":
\"ENOTFOUND\",\n \"errno\":\"ENOTFOUND\",\n \"syscall\":\:getaddrinfo\"\n}","responseJSON":{"code":"ENOTFOUND","errno":"ENOTFOUND","syscall":"getaddrinfo"},"status":500,"statusText":"error"}
使用的 Javscript:
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('load', this.onDeviceLoad, false);
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceLoad: function() {
window.isAndroid = navigator.userAgent.match(/(android)/gi) != null;
},
onlineEvent: function() {
},
offlineEvent: function() {
},
onDeviceReady: function() {
app.receivedEvent('deviceready');
// online/offline must be set in onDeviceReady
document.addEventListener('online', this.onlineEvent , false);
document.addEventListener('offline', this.offlineEvent , false);
$.ajax({
method: "POST",
url: "http://www.someserver/server.php",
data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
alert( "Data Saved: " + msg.posts );
console.log('NETWORK' + msg.posts);
})
.fail(function(error) {
alert("error: " + JSON.stringify(error));
});
},
receivedEvent: function(id) {
}
};
app.initialize();
编辑:我知道 500 错误是什么。我已经测试了服务器。它适用于 HTML5 和相同的 JQuery AJAX 调用。我的预感是 Phonegap 有一些东西; JQuery 客户端调用和导致错误的服务器之间。
Edit2:服务器名称是私有(private)的。该字符串在我的实际代码中设置为真实服务器。
最佳答案
jQuery 不“连接到互联网”。它只是一个 JavaScript 库。您收到的 500 状态响应告诉您尝试 POST 的服务器出现错误:
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_Server_Error
关于javascript - Phonegap + JQuery 无法连接到互联网,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29153528/