Reputation: 1
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");
Upvotes: 0
Views: 870
Reputation: 151
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
Upvotes: 1