禁止在Cordova 5.0.0应用中做AJAX POST
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.
,它返回{"readyState":0,"status":0,"statusText":"error"}
。
当我通过在本地服务器上托管访问HTML页面时,它可以毫无怨言地工作。(正确响应:"{'"result'":'"ok'"}"
)
当我在Android或iOS设备上运行它作为Cordova App时,我得到{"readyState":4, "responseText":"<!DOCTYPE HTML PUBLIC '" -//IETF//DTD HTML 2.0//EN'">... blah blah ... 403 Forbidden ... , "status":403, "statusText": "Forbidden"}
。
怎么回事?我如何让这个请求在手机应用程序中工作?
当我在服务器上禁用ModSecurity 时,此问题已解决。
对我来说,我可以通过cPanel访问我的主机:
cPanel> Security> ModSecurity
我希望这对别人有帮助。
谢谢你@jcesarmobile指出问题出在服务器上
- 如何使用自定义标头跨域执行AJAX POST
- 为什么 AJAX POST 请求不起作用
- Ajax POST请求没有'我不了解PHP
- 数据未从ajax POST返回
- AJAX Post的奇怪Javascript/PHP行为
- 使用javascript ajax post方法的未定义偏移PHP错误
- PHP/Ajax/jquery/JON-在Ajax Post之后将echo文本中的一部分作为变量返回
- Ajax Post请求不起作用
- 使用AJAX POST从javascript到带有Strong参数的Rails4控制器
- jQuery AJAX Post方法错误(语法错误)
- Jquery Ajax POST不工作.适用于GET
- 正在拆分AJAX POST变量
- 发送jquery-ajax$.post后重置连接
- ajax post在webapi中传递null参数
- 如何在javascript中使用ajax post更改密码
- 传递单元格值以便使用ajax post更新表
- 如何使用带有动态接受好友按钮的ajax post更新更多的表
- 如何将AJAX POST转换为HTTP POST,以便在单击时发布
- Ajax POST返回成功,但没有;t更新JSON文件
- 通过 ajax POST 向 PHP 发送两个值以查询 SQL 数据库