簡體   English   中英

如何從ajax調用返回多個值/數組?

[英]How to return multiple values / array from an ajax call?

我想知道在php中處理ajax調用后,傳遞多組數據(字符串)的正確方法是什么。

我知道echo是用來發送回一串數據的,但是如果我想發送多個字符串怎么辦? 還有如何成功處理這些字符串:function(html){}?

將結果數組編碼為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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM