简体   繁体   English

如何在AJAX响应中返回数组和HTML数据?

[英]How can I return an array and HTML data in an AJAX response?

I have an array like this in my PHP page named new1.php: 我的PHP页面中有一个名为new1.php的数组:

$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

In the calling page, when I console.log(data.html) it gives undefined . 在调用页面中,当我console.log(data.html)它给出undefined The same happens for console.log(data.array); console.log(data.array);发生同样的情况console.log(data.array); . Here is my AJAX code: 这是我的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);
    }
});

Most importantly, I want to know what is the best way to return a page with other data from AJAX response? 最重要的是,我想知道从AJAX响应中返回包含其他数据的页面的最佳方法是什么?

You should be json parse, because you are json encoding from php file, and as there is data type of your ajax is text so you need to parse the json. 您应该进行json解析,因为您使用的是php文件中的json编码,并且由于ajax的数据类型是文本,因此您需要解析json。

 $.ajax({
         url:"new1.php",
         type:"POST",
         data:{somedata:somedata},
         dataType:"text",
         success: function(data){
                data = JSON.parse(data);
                  console.log(data);
                console.log(data.html);
                console.log(data.array);

            }
    });

from your php code where you do json_encode add this to the top of the page header("Content-Type: application/json"); 从您执行json_encode的php代码中,将其添加到页面header("Content-Type: application/json");的顶部header("Content-Type: application/json"); then your encode should take in array as parameter instead 那么您的编码应该采用数组作为参数

json_encode(array("array"=>$arr, "html"=>$html));

it should see your record as json now and please change ur dataType to json from Jquery intelligence guess from the server state ( jquery ) it will automatically take json instead 它应该现在将您的记录显示为json ,请从服务器状态( jquery )从jQuery智能猜测将ur dataType更改为json ,它将自动采用json代替

dataType (default: Intelligent Guess (xml, json, script, or html)) Type: String The type of data that you're expecting back from the server. dataType (默认值:Intelligent Guess(xml,json,脚本或html))类型:String期望从服务器返回的数据类型。 If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). 如果未指定,则jQuery将尝试根据响应的MIME类型来推断它(XML MIME类型将产生XML,在1.4中,JSON将产生JavaScript对象,在1.4脚本中,脚本将执行该脚本,而其他任何内容将是以字符串形式返回)。 The available types (and the result passed as the first argument to your success callback) are: 可用的类型(以及作为第一个参数传递给您的成功回调的结果)是:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM