【发布时间】:2016-08-04 23:40:21
【问题描述】:
我正在尝试将 PHP API 调用的 JSON 结果传递给将在视图中显示结果的函数。
script.js函数
function ajaxProductsSearch() {
products.empty();
preloader.css('visibility', 'visible')
// Issue a request to the proxy
$.post('test.php', {
'search': searchBox.val()
}
function($results) { // pass $results from test.php?
if ($results.results_count == 0) {
preloader.css('visibility', 'hidden');
message.html("We couldn't find anything!").show();
return false;
}
$.each() { // code to display in view?
// var html = '';
// products.append(html);
};
preloader.css('visibility', 'hidden');
}, 'json');
}
基本上,您在视图的文本框中输入搜索词,script.js 会将字符串发布到test.php 脚本,然后该脚本将运行 API 查询请求并通过echo $results 显示结果。
【问题讨论】:
-
有什么问题...?
-
你错过了函数($results)和 $.post 的其余部分之间的逗号。
-
它在做什么而不是进行 AJAX 调用? Javascript 控制台中是否有任何错误?
标签: javascript php json post