2

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));
        }


Request/Response Headers:
enter image description here enter image description here

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Abhinav Tyagi
  • 5,158
  • 3
  • 30
  • 60
  • The problem should be indeed on the server side, which is not responding correctly to the preflight request (basically it is an empty request, but indeed most articles don't make it easy to understand). From your question, it is not clear to me what's on the server side, Ok, it is php, but is it ready to respond to the preflight? (e.g. [this](https://www.dinochiesa.net/?p=754)) – lrnzcig Aug 24 '16 at 12:21
  • I don't have access to server code :( Need to ask my colleague to provide details. However, there are two things to note: 1. NginX server and 2. PHP Service. PHP service run on NginX server. Since I can see Response Headers getting visible in above screenshots, I believe NginX is fine and my request is landing on PHP service. On PHP we added Access-Control-Allow-XXXX headers for response and then we received 406 error. – Abhinav Tyagi Aug 24 '16 at 14:24
  • Hint: it happened to me once building a server (in java, not php) and I was not handling the preflight request, i.e. I was not allowing a GET with no parameters (or whatever the preflight request was). Note that you will get a `GET` in your php service and you'll have to respond to that. I think I was getting 406, but honestly not sure. – lrnzcig Aug 24 '16 at 14:33
  • That could be the issue as I remember the code allowing POST not sure of GET. Will look into it. – Abhinav Tyagi Aug 24 '16 at 14:46

0 Answers0