I am facing an issue with my ajax call to the Rails server. The response from the server varies between HTML and JSON based on the content type. Surprisingly, this works smoothly on iPhone and desktop browsers like Chrome, but I am encountering a problem on the Android browser. It seems that the server is not recognizing the content type correctly. I suspect that the overrideMimeType function is not working as expected!
Is there any alternate solution available? If I am unable to resolve this, I might have to create a separate URL to handle JSON requests.
The code snippet in question looks like this :
function makeAjaxCall {
xmlhttp=new XMLHttpRequest();
targetUrl = window.location.pathname;
xmlhttp.open('GET',targetUrl,true);
xmlhttp.overrideMimeType("application/json");
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
// Issue: Response is received as HTML instead of JSON on Android browsers
alert('response:' + xmlhttp.responseText);
r = eval('(' + xmlhttp.responseText + ')');
// Handling additional operations
}
}
xmlhttp.send();
}