我有我的PHP页面这样的数组命名new1.php:如何在AJAX响应中返回数组和HTML数据?
$arr = ['value 1', 'value 2', 'value 3'];
$html = '<div>huge data with all tags like a page</div>';
$response = json_encode('array' => $arr, 'html' => $html);
echo $response
在调用页面,当我console.log(data.html)
它给undefined
。 console.log(data.array);
也是如此。这里是我的AJAX代码:
$.ajax({
url: "new1.php",
type: "POST",
data: { somedata: somedata },
dataType: "text",
success: function(data) {
console.log(data);
console.log(data.html);
console.log(data.array);
}
});
最重要的,我想知道的是返回与AJAX响应其他数据的网页的最佳方式?
'dataType:“text”,'change to'dataType:“json”,' – guradio
@JonStirling,没有语法错误 – EaB
@guradio,如果我这样做,我将能够获得HTML页面或HTML内容 – EaB