I am getting a CORS issue only on axios.
In this html page, I am using a traditional form to post from domain A to domain B.
The form submit works correctly. The ajax post request works correctly. The axios post returns me to a CORS .
Access to XMLHttpRequest at 'https://domainB/post.php' from origin 'http://domain A.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource
<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>
my post.php
<?php
echo 'post working';
var_dump( $_POST);
$_POST['username'];
?>