gpt4 book ai didi

php - 使用 jQuery/AJAX 从 php POST 接收返回 JSON

转载 作者:行者123 更新时间:2023-12-01 07:16:55 25 4
gpt4 key购买 nike

我第一次尝试使用搜索建议在示例中做出 POST 响应。这是我到目前为止所拥有的:

$('#search').keyup(function() {
var searchField = $('#search').val();
if (searchField.length > 1) {

$.ajax({
url : "search_query.php",
type : "POST",
dataType: "json",
data : {
query : searchField
},
success : function(data) {
var output = '<div>';
$.each(data, function(key, val) {
if (val.item.search(searchField) != -1) {
output += '<p><a href="#">' + val.item + '</a></p>';
};
});
$('#search_results').html(output);
}
})
};
});

谁能告诉我如何正确解析 JSON 响应?

最佳答案

您尚未将 output 定义为变量,以下内容应该有效。

$('#search').keyup(function() {
var searchField = $('#search').val();
if (searchField.length > 1) {

$.ajax({
url : "search_query.php",
dataType : "json",
type : "POST",
data : {
query : searchField
},
success : function(data) {
var output = '';
$.each(data, function(key, val) {
if (val.item.search(searchField) != -1) {
output += '<p><a href="#">' + val.item + '</a></p>';
};
});

}
})
};
});

关于php - 使用 jQuery/AJAX 从 php POST 接收返回 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17912277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com