阿贾克斯多阵列回报 [英] Ajax Multiarray return

查看:100
本文介绍了阿贾克斯多阵列回报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着在此刻获得从阿贾克斯邮政JSON请求的响应多阵列。
之后改掉几个小时,现在IAM希望在这里得到帮助...

I try at the moment to get a Multiarray response from a ajax Post json request. After hours of trys iam now hope to get help here...

JS Ajax响应

 var data = {
  "myid": "1234"
};
 $(".the-return").html("");
$.ajax({
  type: "POST",
  dataType: "json",
  url: "../post_test/ajax.php", 
  data: data,
  success: function(data) {

            $.each(data, function (i, item) {
                $(".the-return").append("JSON: " + data["messages"]+"<br>");
            });



    alert("Form submitted successfully.\nReturned json: " + data["json"]);
  }
})
return false;

结果
PHP

$personen = array( 
    //for each loop from Database 
    array("message","from","to ","datetime"),
    //pushing a Array in a Array ????? 
);

所以我的问题是,现在... 的结果
- >如何稳定阵列返回阿贾克斯成功的功能?结果
- >以及如何阅读在Sucess函数的返回,所以我可以有结果添加它
结果

So my Question is now...
-> How to return a stable Array to the Ajax Success Function?
-> And how to read the Return at the Sucess Function so i can Add it with

$("#chatbox_"+chatboxtitle+" .chatboxcontent").append('<div class="chatboxmessage"><span class="chatboxmessagefrom">'+item.f+':&nbsp;&nbsp;</span><span class="chatboxmessagecontent">'+item.m+'</span></div>');

结果

我已经在谷歌搜索约40网站关于数组和Ajax的回报,但亚姆无法找出正确的方式。

I already searched about 40 Sites in Google about Arrays and Ajax Returns but iam not able to figure out the right Way.

我尝试用

$.each(data.items, function(i,item){

和PHP的网站

    $items .= <<<EOD
                   {
        "s": "0",
        "f": "{$chat['from']}",
        "m": "{$chat['message']}"
   },

EOD;

Crashs阿贾克斯的触发completetly ...

Crashs the Trigger of the Ajax completetly ...

感谢
所有的答案

THANKS for all Answers

修改
其恩$ C $光盘已在PHP
- >我需要JS侧喜欢的事
   在从阿贾克斯响应消息的每个消息
      消息是从TEXT去

EDIT Its Encoded already in PHP -> I need something on JS Side like for each message in messages from ajax response message is FROM going TO with TEXT

推荐答案

怎么做例子。文件 varex2.php 返回数组的JSON格式的数组。文件 varex1.php 德codeS JSON数据并将其显示在警报窗口。为了测试下一个codeS,创建两个文本文件,使用给定的名称,复制粘贴codeS,打开浏览器,并运行本地主机/ varex1.php ,在这里,他们分别是:

Example of how to do it. File varex2.php returns an array of arrays in JSON format. File varex1.php decodes the JSON data and displays it in "alert" windows. To test next codes, create two text file, use the given names, copy-paste the codes, open your browser and run localhost/varex1.php, here they are:

varex2.php

<?php

// THIS IS AN ARRAY OF ARRAYS.
$products = Array( Array( "code"  => "0401",
                          "name"  => "shoes",
                          "price" => 700
                        ),
                   Array( "code"  => "0992",
                          "name"  => "shirt",
                          "price" => 250
                        ),
                   Array( "code"  => "5800",
                          "name"  => "glasses",
                          "price" => 400
                        )
                 );
$json = json_encode( $products );
echo $json;  // ARRAY RETURNED IN JSON FORMAT.

?>

varex1.php

<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script type = "text/javascript">
function myAjax () {
$.ajax( { type : 'POST',
          data : { },
          url  : 'varex2.php',
          success: function ( data ) {
            var i;
            var obj = JSON.parse( data );       // DECODE JSON DATA.
            for ( i = 0; i < obj.length; i++ )  // WALK THE SUB-OBJECTS.
              alert( obj[ i ].code + "\n" +
                     obj[ i ].name + "\n" +
                     obj[ i ].price  );
          },
          error: function ( xhr ) {
            alert( "error" );
          }
        });
}
    </script>
  </head>
  <body>
    <button onclick="myAjax()">Click here to get the data</button>
  </body>
</html>

这篇关于阿贾克斯多阵列回报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆