我有一個非常簡單的科爾多瓦應用程序,它試圖做一個AJAX POST。403禁止從科爾多瓦5.0.0應用程序進行AJAX POST
請求的URL,simple.php,看起來是這樣的:
<?php
header('Access-Control-Allow-Origin: *');
die (json_encode (array('result'=>'ok')));
?>
index.html的正是這樣的:
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function() {
$.ajax(
{
type: 'POST',
url: 'http://drawonthe.net/simple.php',
contentType: 'text/plain',
xhrFields: {
withCredentials: false
},
success: function(res) {
alert('got this res : ');
alert(JSON.stringify(res));
},
error: function(err) {
alert('got this fail : ');
alert(JSON.stringify(err));
}
});
});
</script>
</head>
<body>
</body>
我的科爾多瓦config.xml文件包括以下內容:
<content src="index.html" />
<access origin="*" />
<allow-intent href="*" />
<allow-navigation href="*" />
<access origin="content:///*" />
我的應用程序包括cordova-plugin-whitelist
插件。
我已經在index.html中嘗試過各種CSP,但沒有很好的結果。例如。
<meta http-equiv="Content-Security-Policy" content="default-src *; script-src * 'unsafe-eval' 'unsafe-inline'; connect-src *; img-src *; style-src * 'unsafe-inline' ; media-src *; ">
當我在本地訪問這個HTML頁面(從文件://)我得到XMLHttpRequest cannot load http://drawonthe.net/simple.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 403.
並返回{"readyState":0,"status":0,"statusText":"error"}
。
當我通過託管在本地服務器上訪問HTML頁面時,它的工作原理沒有任何不滿。 (正確答案:"{\"result\":\"ok\"}"
)
當我在Android或iOS設備上將它作爲Cordova應用程序運行時,我獲得了{"readyState":4, "responseText":"<!DOCTYPE HTML PUBLIC \" -//IETF//DTD HTML 2.0//EN\">... blah blah ... 403 Forbidden ... , "status":403, "statusText": "Forbidden"}
。
發生了什麼事? 我如何通過手機應用程序提出此請求?
你在哪裏訪問這個html頁面?在您的瀏覽器或手機瀏覽器中?它在哪裏顯示成功? – BBdev
如果我從本地服務器運行它 - 實際上從任何服務器運行它 - 例如,如果我將它上傳到這裏:http://brianmartin.com.au/index_so_demo.html – kris
我看不出你爲什麼做了什麼將無法工作,但也許你可以有cordova-plugin-legacy-whitelist替代cordova-plugin-whitelist的更多運氣? – QuickFix