$ajax 获取返回值object,来自.ajax()调用的数据的jQuery .find()返回“ [object Object]”,而不是di...

来自.ajax()调用的数据的jQuery .find()返回“ [object Object]”,而不是di

尝试使用.find()从.ajax()返回的数据中查找具有id="result"的div元素。不幸的是,alert(result)不返回div#result。

这是我的代码:

$.ajax({

url: url,

cache: false,

success: function(response) {

result = $(response).find("#result");

alert(response); // works as expected (returns all html)

alert(result); // returns [object Object]

}

});

Mensch asked 2020-01-26T17:22:28Z

18个解决方案

104 votes

要具体回答您的问题,它似乎工作正常。 您说过它返回find(),这是jQuery将使用filter()方法返回的结果。 它返回与find查询匹配的jQuery元素。

尝试获取该对象的属性,例如find()-它应返回filter()。

通常,此答案取决于find()是否是顶级元素。

如果find()是顶层元素,

Text

find()无法正常工作。 而是使用filter():

var $result = $(response).filter('#result');

如果find()不是顶层元素,

Text

find()将工作:

var $result = $(response).find('#result');

Stephen Watkins answered 2020-01-26T17:23:09Z

28 votes

我只花了3个小时来解决类似的问题。 这对我有用。

我尝试从$.get响应中检索的元素是body标签的第一个子元素。 由于某种原因,当我在该元素周围包裹一个div时,它可以通过$(response).find('#nameofelement')检索到。

不知道为什么,但是,是的,可检索元素不能成为身体的第一个孩子……可能对某人有用:)

LiveSource answered 2020-01-26T17:23:38Z

20 votes

尝试这个:

result = $("#result", response);

btw alert是一种粗略的调试方法,请尝试console.log

antpaw answered 2020-01-26T17:24:02Z

8 votes

这是你的答案:

Hello
World

以下jQuery无法正常工作:

$(data).find('div.test');

由于div是顶级元素,数据不是元素而是字符串,因此要使其正常工作,您需要使用.filter

$(data).filter('div.test');

另一个相同的问题:在$ .AJAX加载的HTML上使用Jquery选择器?

Bảo Nam answered 2020-01-26T17:24:35Z

6 votes

不要忘记用html解析。 喜欢:

$.ajax({

url: url,

cache: false,

success: function(response) {

var parsed = $.parseHTML(response);

result = $(parsed).find("#result");

}

});

必须工作:)

funny answered 2020-01-26T17:24:59Z

4 votes

这对我有用,您只需要将.html()放在末尾-$(response).find(“#result”)

Steve Mather answered 2020-01-26T17:25:21Z

3 votes

jQuery find()返回一个包装DOM对象的jQuery对象。 您应该能够使用该对象来完成div所需的操作。

Ryan answered 2020-01-26T17:25:41Z

2 votes

问题是您的ajax响应返回一个字符串,因此如果直接使用$(response),它将返回JQUERY:未捕获错误:语法错误,控制台中无法识别的表达式。 为了正确使用它,您需要首先使用一个名为$ .parseHTML(response)的JQUERY内置函数。 正如函数名称所暗示的那样,您需要首先将字符串解析为html对象。 就像您的情况一样:

$.ajax({

url: url,

cache: false,

success: function(response) {

var parsedResponse = $.parseHTML(response);

var result = $(parsedResponse).find("#result");

alert(response); // returns as string in console

alert(parsedResponse); // returns as object HTML in console

alert(result); // returns as object that has an id named result

}

});

sturzerblitz answered 2020-01-26T17:26:02Z

1 votes

您可以通过这种方式找到任何div并获取其属性或所需的任何内容。

$(response).filter('#elementtobefindinresponse').attr("id");

要么

$(response).filter('img#test').attr("src");

Heart answered 2020-01-26T17:26:26Z

0 votes

响应HTML中是#result吗? 尝试以下方法。 如果找不到任何内容,jQuery仍将返回一个空对象。

alert(result.length);

Jason McCreary answered 2020-01-26T17:26:47Z

0 votes

您应该将dataType: "html"添加到请求中。 我很确定,如果它不知道它是html,则将无法搜索返回的html的DOM。

Fabian answered 2020-01-26T17:27:07Z

0 votes

指定console.log("type of response: " + typeof response)。

如果您不这样做,jQuery将会猜测请求的数据类型(请检查:[http://api.jquery.com/jQuery.ajax/)。]我的猜测是,在您的情况下console.log("type of response: " + typeof response)是alert("type of response:" + typeof response)而不是DOMObject。显然是DOM 方法不适用于字符串。

您可以使用console.log("type of response: " + typeof response)(或alert("type of response:" + typeof response),以防不运行Firebug)进行测试

FK82 answered 2020-01-26T17:27:37Z

0 votes

如果您的ajax调用返回JSON数组,服务器端的JSON字符串,那么您应该开始执行以下操作:

$("button#submit").click(function() {

$.ajax({

type: "POST",

url: "ajax_create_category",

data: $('form#create_cat_form').serialize(),

success: function(data)

{

if(data)

{

var jsonObj = JSON.parse(data);

if(jsonObj.status)

{

$("#message").html(jsonObj.msg);

}

}

}

});

});

将您的JSON对象解析为JS对象(变量),然后可以使用数据的索引来检索数据!希望这对你们中的某些人有所帮助! :)

Randika Vishman answered 2020-01-26T17:28:02Z

0 votes

您只需使用以下代码

var response= $(result);

$(response).find("#id/.class").html(); [or] $($(result)).find("#id/.class").html();

karthikeyan ganesan answered 2020-01-26T17:28:22Z

0 votes

$.ajax({

url: url,

cache: false,

success: function(response) {

$('.element').html(response);

}

});

< span class = "element" >

//response

< div id = "result" >

Not found

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值