我想做什么
从本地html / javascript网站发布到在线PHP文件。
问题
当我尝试使用下面的代码时,我不断收到以下提到的错误。
背景:
该网站旨在在本地运行。由于要由每个用户选择使用哪种浏览器,因此我希望找到一种可以解决上述问题的方法,而不必强加对每个用户修改浏览器策略。
如果可能的话,这可能吗?
JavaScript代码:
$.ajax({
type: 'POST',
url: 'http://example.com/test.php',
crossDomain: true,
data: "my_request_is=foo",
dataType: 'json',
success: function(responseData, textStatus, jqXHR)
{
console.log(responseData);
},
error: function (responseData, textStatus, errorThrown)
{
console.warn(responseData, textStatus, errorThrown);
alert('CORS failed - ' + textStatus);
}
});
php代码(test.php):
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type');
echo json_encode(array("your_request_was" => $_POST['my_request_is']));
?>
错误: