admin管理员组文章数量:1355559
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 );
}
});
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 );
}
});
Share
Improve this question
edited May 1, 2016 at 18:26
marc_s
756k184 gold badges1.4k silver badges1.5k bronze badges
asked Oct 31, 2014 at 20:28
Daniel.VDaniel.V
2,4927 gold badges32 silver badges60 bronze badges
6
-
Why are you using
.load()
instead of.get()
if you just want the JSON? – jfriend00 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 – Daniel.V 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. – jfriend00 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??? – Daniel.V 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 remendation is to use the right tool for the job rather than try to hack the wrong tool. – jfriend00 Commented Oct 31, 2014 at 21:01
2 Answers
Reset to default 4i think you have to do something like this:
$("#div").load( "url", function( response, status, xhr ) {
var responseAsObject = $.parseJSON(response);
console.log(responseAsObject.ok);
});
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);
});
本文标签: javascriptHow to get response with load jQueryStack Overflow
版权声明:本文标题:javascript - How to get response with .load jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744018988a2576864.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论