0
votes

How do i set the content-type to application/json from text/html in response header i already set the request header content type to application/json but the response header content-type shown in text/html

here is my code

      function check(c2) {
          document.title = 'Checking';
                    var xmlhttp;
                    xmlhttp=new XMLHttpRequest();
                    xmlhttp.onreadystatechange = function()
                    {
                        if (xmlhttp.readyState==4 && xmlhttp.status==200)
                        {
                            var xdata = xmlhttp.responseText;   
                        if (xdata.match("GOOD")) {
                          $("#GOOD").append(xdata);
                          document.getElementById("goodcnt").innerHTML = (eval(document.getElementById("goodcnt").innerHTML) + 1);
                          $("#listcnt").html(parseInt($("#listcnt").html()) - 1),
                          line('#cs');
                          Check();

                      } else if (xdata.match("BAD")) {
                         $("#BAD").append(xdata);
                         document.getElementById("badcnt").innerHTML = (eval(document.getElementById("badcnt").innerHTML) + 1);
                         $("#listcnt").html(parseInt($("#listcnt").html()) - 1),
                         line('#cs');
                         Check();
                      }
 }
                      }
                    xmlhttp.open("POST","api?list=" + c2 ,true);
                    xmlhttp.setRequestHeader("Content-Type","application/json");
                    xmlhttp.send();
      }

i also tried to add this code in api

header("Content-Type: application/json");
1
Seem to be wanting to make a GET not POST request when you provide query string like that and aren't sending any datacharlietfl
@charlietfl i have curl in api and im getting message that my json is malformedJason Lopera
Input could not be parsed i get this curl response instead of getting the invalid messageJason Lopera
That sounds like a server side problemcharlietfl
@JasonLopera Please add your PHP code. How you are returning the data.?Sami Ahmed Siddiqui

1 Answers

1
votes

When you're using this line:

xmlhttp.setRequestHeader("Content-Type","application/json");

You're saying to the server that the sent content contains a JSON. And then, when the server responds it needs to know that the client supports a JSON in reply.

For this reason, you need to add the line below after:

xmlhttp.setRequestHeader("Accept","application/json");

More info: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept