简体   繁体   English

我们如何在ajax中编码Multi JSON数据响应

[英]How can we encode the Multi JSON data response in ajax

I have a problem with json encoded information which loaded via ajax.I pass the multi JSON to the ajax for display the field value. 我对通过ajax加载的json编码信息有问题。我将多JSON传递给ajax以显示字段值。

How can i fetch the field value from the json using query ajax. 我如何使用查询ajax从json提取字段值。

ajax code : Ajax代码:

                   ....success:function(data){
                    var TotalBuyPrice = 0;
                    var TotalItem = 0;
                    $.each(data, function(c,cart){

                        //Condition follow  1
                      var InStockQty =cart.products_qty;
                      alert(InStockQty);

                         //And also follow  2
                       var name =cart["withoutdiscount"][0]["products_name"];
                        alert(name); 

                    });
                     }...

The PHP code : PHP代码:

These are my steps following for json response.By using array collect the result 这些是我对json响应的以下步骤。通过使用数组收集结果

        $response = array();
        $response['withoutdiscount'] = $withoutdiscount;
        $response['withdiscount'] = $withdiscount;

        echo $_GET['jsoncallback'] . '(' . json_encode($response). ');';

jsoncallback: jsoncallback:

         ({"withoutdiscount":[{"products_id":"1","products_name":"Lumia"}],
            "withdiscount":[{"products_id":"2","discount_qty":"8"},
                            {"products_id":"3","discount_qty":"1"}
                           ]
          });

I Solve the problem like this: Using PHP file get one response like this: 我解决了这样的问题:使用PHP文件得到如下响应:

    $response = array();
    $response['withoutdiscount'] = $withoutdiscount;
    $response['withdiscount'] = $withdiscount;

    echo $_GET['jsoncallback'] . '(' . json_encode($response). ');';

jsoncallback: //JSON response jsoncallback:// JSON响应

     ({"withoutdiscount":[{"products_id":"1","products_name":"Lumia"}],
        "withdiscount":[{"products_id":"2","discount_qty":"8"},
                        {"products_id":"3","discount_qty":"1"}
                       ]
      });

using ajax function call the json response like this: 使用ajax函数调用json响应,如下所示:

        function querySuccess(tx,results) {
            var jsonString = JSON.stringify(results);
            $.ajax({
                url:'getcart.php',
                data: {data : jsonString},
                dataType: 'jsonp',
                jsonp: 'jsoncallback',
                timeout: 5000,
                success:function(data){
                    var withdiscount=data.withdiscount;
                    var withoutdiscount=data.withoutdiscount;
                if(withdiscount!='')
                {
                    $.each(withdiscount, function(c,cart){
                       var discount_qty =cart.discount_qty;
                        alert(discount_qty);
                     );
                 }
                if(withoutdiscount!='')
                {
                    $.each(withoutdiscount, function(c,cart){ 
                       var products_name =cart.products_name;
                          alert(products_name);
                     });
                }
               }
            });
        }

This is working with me. 这正在和我一起工作。

NOTE: There is only 2 JSON response is pass so i given directly. 注意:只有2 JSON响应通过,所以我直接给出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM