4

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 );
    }
});
6
  • Why are you using .load() instead of .get() if you just want the JSON? Commented Oct 31, 2014 at 20:34
  • @jfriend00 Because as I mentioned the server sometimes send a html and .load function work properly but in some case it return a josn and I want to get that json with .load Commented Oct 31, 2014 at 20:36
  • Why not use .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. Commented Oct 31, 2014 at 20:43
  • @ jfriend00 I know what the response of server is I just one to put a condition here and I just one to know is there any way that I can get both html and json with .load(not .get).That was my first place question and the answer is going to be so easy no or yes.if yes how. are you going to answer my question??? Commented Oct 31, 2014 at 20:59
  • .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. Commented Oct 31, 2014 at 21:01

2 Answers 2

4

i think you have to do something like this:

$("#div").load( "url", function( response, status, xhr ) {
    var responseAsObject = $.parseJSON(response);
    console.log(responseAsObject.ok);
});
Sign up to request clarification or add additional context in comments.

Comments

2

Using response to replace #div html content :

$("#div").load("url", function (responseTxt, statusTxt, xhr) {
    if (statusTxt == "success") {
        var responseAsObject = $.parseJSON(response);
        $(this).html( responseAsObject.response );
    }
    if (statusTxt == "error") 
        alert("Error: " + xhr.status + ": " + xhr.statusText);
});

1 Comment

This works great for me. The xhr third parameter was the secret sauce. In my case, I was returning a Task<PartialViewResult> from my Action Method. I just changed that to Task<IActionResult>, and returned a BadRequest(message), which turns into an xhr.status of 400. Everything else worked as usual.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.