【发布时间】:2016-12-20 09:59:15
【问题描述】:
我有一个简单的搜索表单,它使用 jquery 查询外部服务器的结果
$("#formsearch").on("submit", function (event) {
// everything looks good!
event.preventDefault();
submitFormSearch();
});
function submitFormSearch(){
// Initiate Variables With Form Content
var searchinput = $("#searchinput").val();
$.ajax({
type: "GET",
url: "https://external-server/api/",
headers: {"Authorization": "xxxxxxxxxxxxxx"},
data: "action=Search&query="+searchinput,
success:function(json){
console.log(json);
$.ajax({
type: "POST",
url:'search_func.php',
data: "func=parse&json="+json,
success:function(data) {
console.log(data);
$('#risultato_ricerca').html(data);
}
});
}
});
}
第一个 GET ajax 工作正常,我得到了正确的数据,但试图在 post 中将此 json 数据发送到我的 php 脚本,但我无法获取数据。 这是 search_func.php 中的代码
if(isset($_POST['func']) && !empty($_POST['func'])){
switch($_POST['func']){
case 'parse':
parse($_POST['json']);
break;
default:
break;
}
}
function parse($json) {
$obj = json_decode($json,true);
var_dump($obj);
}
... 它显示 NULL
我哪里错了?
编辑: 已解决
更改:data: "func=parse&json="+json,
至:
data: { func: 'parse', json: JSON.stringify(json) },
json 代码正确传递给 search_func.php 将 php 文件中的函数解析更改为:
function parse($json) {
$data = json_decode(stripslashes($json),true);
print_r($data);
}
谢谢。
【问题讨论】:
-
$json 有什么值?
-
$json in function get value from $_POST['json'] in parse($_POST['json']); $_POST['json'] 在 jquery 中获取数据中的值:"func=parse&json="+json, console.log(json);显示
codeObject {状态:“ok”,格式:“json”,查询:“acquari”,sort_by:“”,sort_order:“desc”…}code -
为什么需要 2 个 ajax 调用?
-
1° 从外部服务器获取搜索回复 2° 在 php 中详细说明结果
标签: javascript php jquery json ajax