我不明白为什么,但我的CORS电话无效。
我将meta标签添加到index.html
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
我的Android清单有:
<uses-permission android:name="android.permission.INTERNET" />
我的电话标题是:
Access-Control-Allow-Headers → Content-Type
Access-Control-Allow-Methods → GET,PUT,POST,DELETE
Access-Control-Allow-Origin → *
在我的config.xml中,我有
<access origin="*" />
<plugin name="cordova-plugin-whitelist" version="1" />
我的代码是
var request = new XMLHttpRequest();
request.open("GET", "http://XXXXXXXXXX/XXXXX", true);
console.log("test");
request.onreadystatechange = function () {
console.log("DOES NOT COME HERE?")
if (request.readyState == 4) {
if (request.status == 200 || request.status == 0) {
alert(code);
var product = JSON.parse(request.responseText);
alert(product.CNK);
}
}
&#34;不会来这里&#34;没有打印,日志中也没有打印错误?我真的不知道自己做错了什么:(
答案 0 :(得分:1)
好的解决方案是如此愚蠢,我搜索的方式是为了这个
var request = new XMLHttpRequest();
request.open("GET", "http://XXXXXXXXXX/XXXXX", true);
console.log("test");
request.send()
request.onreadystatechange = function () {
console.log("DOES NOT COME HERE?")
if (request.readyState == 4) {
if (request.status == 200 || request.status == 0) {
alert(code);
var product = JSON.parse(request.responseText);
alert(product.CNK);
}
}
如您所见,我添加了request.send():)