简体   繁体   中英

How to get php array as a javascript array through ajax

I have a php file called by ajax, where I printed an array, and I want to get the array in the ajax success event and use as javascript array to prepend as valu in two fields with jquery. I tried it as bellow but failed. actually I am new in coding, Pls help me any one....

the php file is as bellow:

$qry = $crud->select("latest_event", "bnDescription, eventHeading","eventID='{$eventID}'");

$data = mysql_fetch_assoc($qry);

$arr = array("content" =>$data['bnDescription'], "heading" => $data['eventHeading']);

header('Content-type: application/x-json');

echo json_encode($arr);

?>

the javascript is:

 $.ajax({ type: "POST", url: "getEventData.php", data:"eventID="+eventID+"&lang="+lang, cache: false, success: function(data){ $("input#eventHeading").prepend(data[heading]); $("textarea#cont").prepend(data[content]); } 

});

data[heading]

You don't have a heading variable.

To get the property with that name, simply write

data.heading

From what I can see in your code, you are returning valid json from your php, but it seems that you have not told $.ajax() what kind of data is being returned. You need to set dataType: 'json' in your $.ajax() call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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