2013-09-18 5 views
0

PHP에서 ajax 호출을 처리 한 후에 여러 세트 (문자열)의 데이터를 전달하는 적절한 방법이 무엇인지 궁금합니다.ajax 호출에서 여러 값/배열을 반환하는 방법은 무엇입니까?

에코가 데이터 문자열을 보내는 데 사용된다는 것을 알고 있지만 여러 문자열을 보내려면 어떻게해야합니까? 또한 어떻게 그 문자열을 성공적으로 처리 할 수 ​​있을까요? function (html) {}?

+1

개체 또는 배열에 포장하고 직렬화하여 보냅니다. – BlitZ

+0

@CORRUPT - JS에서 어떻게 열 수 있습니까? – ShadowZzz

답변

3

결과 배열을 JSON 형식으로 인코딩하고 응답을 반환하십시오.

<?php 
$arr = array ('response'=>'error','comment'=>'test comment here'); 
echo json_encode($arr); 
?> 

//the script above returns this: 
{"response":"error","comment":"test comment here"} 

<script type="text/javascript"> 
$.ajax({ 
    type: "POST", 
    url: "process.php", 
    data: dataString, 
    dataType: "json", 
    success: function (data) { 
     if (data.response == 'captcha') { 
      alert('captcha'); 
     } else if (data.response == 'success') { 
      alert('success'); 
     } else { 
      alert('sorry there was an error'); 
     } 
    } 

}); 
</script> 
+0

감사합니다! 정확히 내가 뭘 찾고 있었는지 ... – ShadowZzz

관련 문제