개발! 딱 깔끔하고 센스있게!

어떻게 ajax 그룹을 php 에서 javascript 본문

개발 스크랩 메모/PHP

어떻게 ajax 그룹을 php 에서 javascript

렉사이 2020. 12. 29. 02:36

이 ajax 코드 있어요.

xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById('addIO').innerHTML+=xmlhttp.responseText; } } xmlhttp.open("GET","http://localhost/Mar7ba/Ontology/getRelatedConceptsAndRelations/3/TRUE",true); xmlhttp.send(); 

저 php 팀 있어요.

$cars = array('사보','볼보','보마','도요타')

어떻게 배열 cars 를 나의 javascript 에 보낼 수 있습니까?

대답

필리핀 피소

echo json_encode($cars); 

자바스크립트

로컬:

var foo = JSON.parse(xmlhttp.responseText); 

jQuery 사용하기:

var foo = $.parseJSON(xmlhttp.responseText); //or $.getJSON("url", function(data){ //data is your array }); 

업데이트

if(xmlhttp.readyState==4 && xmlhttp.status==200){ //document.getElementById('addIO').innerHTML+=xmlhttp.responseText; var cars = JSON.parse(xmlhttp.responseText); //cars will now be the array. //Do whatever you want here. $("#addIO").html(cars.join(", ")); //Join array with ", " then put it in addIO } 

jQuerry 사용하려면 < head >

 
Comments