如何从jQuery调用中获取数组?

How can I make / retrieve array from jQuery Call

本文关键字:获取 数组 调用 jQuery      更新时间:2023-09-26

Jquery Ajax调用在数据库上插入新记录。文件被调用(php)需要返回两个值,插入的内容和它的id。

我应该如何发送回这些值(数组,xml, echo)在php上?

我将如何在Jquery中分离这些Ajax成功(数据)?

当前处理php文件只返回一个字符串作为插入的内容,然后我用它更新视图。需要返回记录id,以便我可以连接元素。

如果需要进一步澄清,请告知。谢谢你

JQUERY

$.ajax({
                    type:'post',
                    url: 'posts_in.php',
                    data: $("#postentry").serialize(),
                    success:function(data){
                        var id='posts';
                        //split returned data here
                        $('<div></div>').attr('id',id).addClass('boxee').html(data).prependTo('#boxer');
                        $('#tittle_ent').val('').focus();
                    }
PHP

echo $value1,$value2;
print_r($arrayhere);
XML ?

返回用json_encode()编码的JSON字符串。

$php_array_result = array(1,2,3,4,5,6);
// Send the correct MIME header and echo out the JSON string
header("Content-type: application/json");
echo json_encode($php_array_result);
exit();

在jQuery ajax调用中,添加dataType: "json"属性

$.ajax({
   type:'post',
   url: 'posts_in.php',
   dataType: "json",
   success: function(data) {
      console.dir(data);
   },
   // etc...

如何返回JSON字符串?在服务器端使用json_encode,在jQuery上使用jQuery. parsejson()。

我建议您使用JSON编码数组(json_encode())对数据进行编码,并将其回显到响应中。JSON是将数据传递回客户端的标准和常用方法。
使用jquery,您可以使用$.parseJSON()对数据进行反序列化,如下所述。