[英]Not able to print PHP JSON array in jQuery
我試圖打印PHP返回的JSON數組在jQuery中打印,但沒有運氣。
PHP代碼
$json = array();
$res = "";
foreach($cor as $service){
$res .= "Some Divs.... long HTML response here";
}
$json['responses'] = $res;
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
jQuery代碼
$(document).delegate('#button', 'click', function() {
var $this = $(this);
$.ajax({
url: 'url',
type: 'POST',
dataType: 'json',
data: $("#form").serialize(),
beforeSend: function() {
$this.button('loading');
},
complete: function() {
$this.button('reset');
},
success: function(json) {
$("#resp").html(json['responses']);
console.log(json['responses']);
},
error: function(data, status, error ) {
console.log(data);
console.log(status);
console.log(error);
}
});
});
在上面,我在控制台console.log(json['responses']);
獲得輸出console.log(json['responses']);
。 我可以在控制台中看到正確的HTML呈現。
但Div與$("#resp").html(json['responses']);
什么都沒有顯示。 :(
#resp
,是的,它存在於頁面上。
這不是奇怪和令人沮喪。 :(
網絡標簽截圖:
1-使用“ echo json_encode($json);
”而不是這個
$ this-> response-> addHeader('Content-Type:application / json');
$這 - >響應 - > setOutput(json_encode($ JSON));
2-更改以下代碼
success: function(json) {
$("#resp").html(json.responses);
console.log(json.responses);
},
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.