I am working with .load function in jQuery to load a html page into a div with ajax but I have problem since server always doesn't send a html, sometimes it send a json and I want to get that json with .load.
I thought it would be a response in callback function in .load but it return undefined and just put that json into the div so how can I get that json with .load.
The server send this json :
{ok : false}
Here is my jQuery code:
$( "#div").load( "url", function( response, status, xhr ) {
////how can I get the false result here response.ok return nothing!!!
console.log(response.ok);
if ( status == "error" ) {
var msg = "Sorry but there was an error: ";
$( "#div" ).html( msg + xhr.status + " " + xhr.statusText );
}
});
.load()instead of.get()if you just want the JSON?.get()and then you can examine the return data type and if the server returns HTML, you can then put that in the DOM (like.load()would do), but if it returns JSON, then you have the JSON response already. It seems a bit odd that you are making a request and don't know what the server is going to return - that isn't usually the case..load()is simply not what you want to use to fetch JSON from a server. It's the wrong tool for the job. My recommendation is to use the right tool for the job rather than try to hack the wrong tool.