I am trying to consume a PHP service using XMLHttpRequest in Javascript.
Receiving following error:
XMLHttpRequest cannot load http://***/adService.php. Response for preflight has invalid HTTP status code 406
Earlier I had CORS error which was corrected by adding Access-Control-Allow-Origin: *
in PHP service.
What can be the issue now and how I can correct it?
I have checked different articles on preflight but couldn't understand them as I am not a backend guy.
JS Code:
function authenticate(){
var user = document.getElementById("user").value;
console.log("User : "+user);
var pass = document.getElementById("pass").value;
var xmlhttp = new XMLHttpRequest(); // new HttpRequest instance
xmlhttp.open("POST", "http://****/adService.php");
xmlhttp.setRequestHeader("Content-Type", "application/json");
xmlhttp.setRequestHeader("Accept", "application/json");
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState != 4) return;
if (xmlhttp.status != 200) {
alert('HTTP error ' + xmlhttp.status);
return;
}
var response = xmlhttp.responseText;
console.log("response->"+response);
data.resp = JSON.parse(response);
if(data.resp.message=='Authentication success'){
alert('That worked!');
}else{
alert('That didn\'t work!');
}
}
var str = '{"id":"'+user+'","password":"'+pass+'","groupName":"","groupAuthorization":false}';
/* console.log("request->"+str); */
xmlhttp.send(JSON.stringify(str));
}