[英]403 Forbidden doing AJAX POST from Cordova 5.0.0 app
我有一個非常簡單的Cordova應用程序,它嘗試執行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>
我的Cordova 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.
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.
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"}
。
這是怎么回事? 以及如何通過電話應用程序使此請求生效?
當我在服務器上禁用ModSecurity時,此問題已解決。
對我來說,我可以通過對主機的cPanel訪問來做到這一點:
cPanel>安全性> ModSecurity
我希望這對其他人有幫助。
感謝@jcesarmobile指出問題必須出在服務器上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.