繁体   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