当我在 iOS10 上使用 Phonegap 应用程序测试一个简单的 AJAX 请求时失败。
代码如下(在浏览器中完美运行):
if($.trim(firstname).length > 0 & $.trim(email).length > 0 & $.trim(password).length > 0) {
console.log("input checked");
$.support.cors = true;
$.ajax({
type: "POST",
url: "http://my-server.com/signup.php",
data: dataString,
crossDomain: true,
cache: false,
beforeSend: function(data) {
console.log("connecting to server");
},
//display success/fail message - put something in data on server
success: function(data, textString, xhr) {
if(data == "success") {
localStorage.login = "true";
localStorage.email = email;
mainView.router.loadPage("swipe.html");
} else if(data == 'exist') {
myApp.alert("Account already exists", "Login Failed");
}
},
error: function(xhr, ajaxOptions, errorThrown) {
console.log("error msg: ");
console.log(xhr);
console.log(errorThrown);
myApp.alert("Unknown error, please try again", "Login Failed");
},
complete: function(data) {
if(data.readyState == "0") {
console.log("unsent");
} else if(data.readyState == "4") {
console.log("done");
}
}
});
} return false;
}
我的 config.xml 文件设置完全按照之前许多答案中的概述:
<plugin name="cordova-plugin-whitelist" />
<access origin="http://my-server.com" />
etc...
显然禁用苹果的应用程序传输安全性是不可能的,因为我不是在构建应用程序,只是在 Phonegap 服务器和 Phonegap 应用程序上测试它。
但这可能与 ATS 阻止 Phonegap 应用程序本身向其他域发出 ajax 请求有关吗?我认为它需要 Phonegap 允许访问所有域,这可能在应用商店中是不允许的!
这是我对正在发生的事情的最佳猜测。有什么想法或解决方法吗?
最佳答案
新 iOS 应用程序的唯一互联网解决方案是将后端移至 https...
关于javascript - AJAX post 请求在浏览器中工作,而不是在 Phonegap 应用程序上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45762409/