我在砌体中使用 ajax
Ajax代码:这个ajax用来获取数据
$.ajax({
type: "get",
url: "some.php",
dataType: "text",
success: function(data) {
if (data && data.length > 0) {
$items = $(data);
$grid.append( $items )
.masonry('appended', $items);
$(this).find(".loading").hide();
}
}
PHP 部分:这只是 php 文件的一小部分或足以理解问题的部分
$b= "sv";
echo "asjdgsajd";
echo "a";
echo $b;
现在我得到的一切都是正确的,但我想使用 $b 的值来设置属性值以及其他值作为内容,但我如何才能特别获得 $b 的值? 谢谢
最佳答案
将 dataType
更改为 json
。
$.ajax({
type: "get",
url: "some.php",
dataType: "json",
success: function(data) {
//data will hold an object with your response data, no need to parse
console.log('Do whatever you want with ' + data.b + '.');
}
在 some.php
中执行以下操作:
$response =array(
'b' => "sv",
'a' => "asjdgsajd",
'c' => "a"
);
echo json_encode($response);
echo $b;
关联数组的项目最终将作为 javascript 对象的属性,您可以在 success
回调中使用它(或 done
函数作为 success
已弃用)。
关于javascript - 从 ajax 响应中获取特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32549836/