I am testing my api as below:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<div onclick="sendData()">Send</div>
<script>
function sendData(){
var sendInfo = {
"name" : "test"
};
$.ajax({
//headers: { 'Access-Control-Allow-Origin': '*' },
beforeSend: function(xhr) {
xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
xhr.setRequestHeader("Content-Type","application/json");
xhr.setRequestHeader("Accept","application/json");
},
url: "http://localhost:8080/someapi",
data: sendInfo,
type: "POST",
dataType: "json",
success: function(data){
//do something after something is recieved from php
},
error: function(){
//bad request
}
});
}
</script>
</body>
</html>
I am getting below error:
Failed to load http://localhost:8080/someapi: Response to preflight request doesn't pass access control check: 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.
How to solve this?