[英]JSON response not objects from php
我正在使用PHP代理繞過跨域Ajax問題
我的PHP:
return array(
'status' => 'ok',
'header' => $header,
'content' => $content
);
所以現在我的$ response ['content']保存了我的內容,但是我也想要我的標題。
如果我:
echo $response['content'];
然后我得到我的回應,如:
這就是我所需要的! 但我也想在回顯中包含$ response [header]。 但是如果我:
echo $response;
我得到:
如何在json響應中同時獲取兩者,然后在JavaScript代碼中使用它? 為什么只返回明文“數組”
如果我使用json_encode,則格式不正確,就像我的第一個示例一樣。
它丟失了所有格式。
您必須使用json_decode()
解析響應數據$content
$data = array(
'status' => 'ok',
'header' => $header,
'content' => json_decode($content)
);
header('Content-type: application/json');
echo json_encode($data);
或者,您可以直接返回JSON,而無需解析
header('Content-type: application/json');
echo '{"status":"ok", "header":"' . $header . '", "content":"' . $content . '"}';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.