在 jQuery 中提取 Ajax 返回数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/400197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extracting Ajax return data in jQuery
提问by venkatachalam
I have done jQuery and Ajax, but I am not able to get the response into a Div element. This is the code:
我已经完成了 jQuery 和 Ajax,但我无法将响应放入 Div 元素中。这是代码:
Index.html
索引.html
$.ajax({
type:"POST",
url: "ajax.php",
data:"id="+id ,
success: function(html){
$("#response").html(data);
}
});
It is receiving the response to my <div id="response"></div>
.
它正在接收对我的<div id="response"></div>
.
The ajax.php
returns following code to the index.html
file:
将ajax.php
下面的代码回报index.html
文件:
<div id ="one"> OneVal </div>
<div id ="sub"> SubVal </div>
Will I able to extract the OneVal and Subval into a variable, and how can I extract "OneVal" and "SubVal", instead of above response?
我能否将 OneVal 和 Subval 提取到变量中,如何提取“OneVal”和“SubVal”而不是上述响应?
回答by redsquare
You can use .filter
on a jQuery object that was created from the response:
您可以.filter
在从响应创建的 jQuery 对象上使用:
success: function(data){
//Create jQuery object from the response HTML.
var $response=$(data);
//Query the jQuery object for the values
var oneval = $response.filter('#one').text();
var subval = $response.filter('#sub').text();
}
回答by redsquare
Change the .find
to .filter
...
更改.find
为.filter
...
回答by VinnyBenson
I have noticed that your success function has the parameter "html", and you are trying to add "data" to your elements html()
... Change it so these both match:
我注意到您的成功函数具有参数“html”,并且您正在尝试将“数据”添加到您的元素中html()
......更改它以使它们都匹配:
$.ajax({
type:"POST",
url: "ajax.php",
data:"id="+id ,
success: function(data){
$("#response").html(data);
}
});
回答by aya
You can use json
like the following example.
你可以json
像下面的例子一样使用。
PHPcode:
PHP代码:
echo json_encode($array);
$array
is array data, and the jQuery code is:
$array
是数组数据,jQuery 代码是:
$.get("period/education/ajaxschoollist.php?schoolid="+schoolid, function(responseTxt, statusTxt, xhr){
var a = JSON.parse(responseTxt);
$("#hideschoolid").val(a.schoolid);
$("#section_id").val(a.section_id);
$("#schoolname").val(a.schoolname);
$("#country_id").val(a.country_id);
$("#state_id").val(a.state_id);
}
回答by Rel
You may also use the jQuery contextparameter. Link to docs
您还可以使用 jQuery上下文参数。链接到文档
Selector Context
By default, selectors perform their searches within the DOM starting at the document root. However, an alternate context can be given for the search by using the optional second parameter to the $() function
选择器上下文
默认情况下,选择器在 DOM 中从文档根开始执行它们的搜索。但是,可以通过使用 $() 函数的可选第二个参数为搜索提供替代上下文
Therefore you could also have:
因此,您还可以拥有:
success: function(data){
var oneval = $('#one',data).text();
var subval = $('#sub',data).text();
}
回答by nOcTIlIoN
on success: function (response) { alert(response.d); }
成功时:功能(响应){警报(响应.d);}