1

我想知道在处理 php.ini 中的 ajax 调用后传递多组数据(字符串)的正确方法是什么。

我知道 echo 用于发送回一串数据,但是如果我想发送多个字符串怎么办?以及如何处理这些字符串 in-success: function(html){} ?

4

1 回答 1

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>
于 2013-09-18T17:02:36.133 回答