【发布时间】:2019-10-05 08:11:04
【问题描述】:
我只在 axios 上遇到了 CORS 问题。
在这个 html 页面中,我使用传统的表单从域 A 发布到域 B。
表单提交工作正常。 ajax 发布请求正常工作。 axios 帖子将我返回到 CORS。
从源 'http://domainA.com' 访问 XMLHttpRequest 在 'https://domainB/post.php' 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:否 'Access-Control-Allow-Origin ' 请求的资源上存在标头
<html>
<head>
<title>Registration Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://npmcdn.com/axios/dist/axios.min.js"></script>
<body>
<h2>Registration Form</h2>
<form action="https://domainB.com/post.php" method="POST">
Last name:
<input type="text" name="username">
<input type="hidden" name="form_submitted" value="1" />
<input type="submit" value="Submit">
</form>
<script>
$(document).ready(function(){
$.post("https://domainB.com/post.php",
{
username: "Donald Duck",
city: "Duckburg"
},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
</script>
<script>
(function() {
axios.post('https://domainB.com/post.php', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
})();
</script>
</body>
</html>
我的帖子.php
<?php
echo 'post working';
var_dump( $_POST);
$_POST['username'];
?>
【问题讨论】: