javascript - How can I return an array and HTML data in an AJAX response? -


i have array in php page named new1.php:

$arr = ['value 1', 'value 2', 'value 3']; $html = '<div>huge data tags page</div>'; $response = json_encode('array' => $arr, 'html' => $html); echo $response 

in calling page, when console.log(data.html) gives undefined. same happens console.log(data.array);. here ajax code:

$.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, want know best way return page other data ajax response?

from php code json_encode add top of page header("content-type: application/json"); encode should take in array parameter instead

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

it should see record json , please change ur datatype json jquery intelligence guess server state (jquery) automatically take json instead

datatype (default: intelligent guess (xml, json, script, or html)) type: string type of data you're expecting server. if none specified, jquery try infer based on mime type of response (an xml mime type yield xml, in 1.4 json yield javascript object, in 1.4 script execute script, , else returned string). available types (and result passed first argument success callback) are:


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -