dongshanfan1941 2015-06-24 23:04
浏览 13
已采纳

Ajax Multiarray返回

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 RESPONSE

 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.
Returned json: " + data["json"]);
  }
})
return false;


PHP

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

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>');


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

My try with

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

And on PHP Site

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

EOD;

Crashs the Trigger of the Ajax completetly ...

THANKS for all Answers

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

  • 写回答

1条回答 默认 最新

  • doumaogui5937 2015-06-24 23:29
    关注

    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 + "
    " +
                         obj[ i ].name + "
    " +
                         obj[ i ].price  );
              },
              error: function ( xhr ) {
                alert( "error" );
              }
            });
    }
        </script>
      </head>
      <body>
        <button onclick="myAjax()">Click here to get the data</button>
      </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大