[英]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.