使用AJAX将JSON结果作为JS数组返回?

时间:2015-07-31 04:45:42

标签: javascript php jquery ajax json

提前道歉 - 我是新手,因此其他答案无法帮助我。

我使用AJAX将数据发送到PHP脚本(第三方API的一部分)。 PHP脚本将结果作为JSON返回,但我不知道如何在我的HTML页面上格式化这些结果。

最后,我想将JSON结果保存为数组,然后使用JS / Jquery在页面上格式化它们。

我不确定如何修改PHP和AJAX脚本来实现这一目标。有人能指出我正确的方向吗?

我的AJAX:

$.ajax({
            type: 'POST',
            url: 'calculator.php',
            data: {toPostcode: toPostcodeValue, parcelLengthInCMs: parcelLengthInCMsValue, parcelHeighthInCMs: parcelHeighthInCMsValue, parcelWidthInCMs: parcelWidthInCMsValue, parcelWeightInKGs: parcelWeightInKGsValue},
            success: function(data) {
                <!--NOT SURE WHAT TO PUT HERE-->
            }
})

PHP(在计算器完成之后 - 不确定是否需要更改):

$serviceTypesJSON = json_decode($rawBody);
echo json_encode($serviceTypesJSON);

预期的JSON结果应如下所示:

{
  "services": {
    "service" : [
      {
        "code": "AUS_PARCEL_REGULAR",
        "name": "Parcel Post (your own packaging)",
        "speed": 2,
        "price": 6.95,
        "max_extra_cover": 5000,
        "extra_cover_rule": "100,1.5,1.5",
        "icon_url": "http://test-static.npe.auspost.com.au/api/images/pac/regular_post_box.png",
        "description": "Based on the size and weight you've entered",
        "details": "Check our ",
        "delivery_time": "Delivered in up to 3 business days",
        "ui_display_order": 1,
        "options": {
          "option": [
            {
              "code": "AUS_SERVICE_OPTION_STANDARD",
              "name": "Standard Service",
              "price": "0.00",
              "price_type": "static",
              "option_type": "optional",
              "ui_display_order": 1
            },
            {
              "code": "AUS_SERVICE_OPTION_SIGNATURE_ON_DELIVERY",
              "name": "Signature on Delivery",
              "price": 2.95,
              "price_type": "static",
              "option_type": "optional",
              "tooltip": "Signature on Delivery provides you with the peace of mind that your item has been delivered and signed for.",
              "ui_display_order": 2,
              "suboptions": {
                "option": {
                  "code": "AUS_SERVICE_OPTION_EXTRA_COVER",
                  "name": "Extra Cover",
                  "option_type": "optional",
                  "price_type": "dynamic",
                  "tooltip": "Extra Cover provides cover for loss, damage or theft of your item and will fully compensate you to the value specified for your item.",
                  "ui_display_order": 1
                }
              }
            }
          ]
        }
      },

2 个答案:

答案 0 :(得分:1)

如果在AJAX调用中返回数据是JSON使用dataType: "json",则可以做两件事。

修改1

如果您使用 dataType: "json" 。如果您确定数据返回是JSON字符串,那么更优选哪个。成功中的data变量将直接为您提供JSON对象。我想你可以像data['services']一样访问它。

success: function (data) {
    jsonObj = $.parseJSON(data);
    //this gives you the inner onject of the return data
    servicesObj = jsonObj.services; 
}

或者您可以获取数据,然后使用 jQuery.parseJSON() 将数据字符串解析为JSON对象。

$.ajax({
    type: 'POST',
    url: 'calculator.php',
    data: {
        toPostcode: toPostcodeValue,
        parcelLengthInCMs: parcelLengthInCMsValue,
        parcelHeighthInCMs: parcelHeighthInCMsValue,
        parcelWidthInCMs: parcelWidthInCMsValue,
        parcelWeightInKGs: parcelWeightInKGsValue
    },
    success: function (data) {
        jsonObj = $.parseJSON(data);
        //this gives you the inner onject of the return data
        servicesObj = jsonObj.services; //or jsonObj["services"]
    }
})

答案 1 :(得分:0)

如果您正在使用,则永远不会调用您的成功函数 echo json_encode();在你的PHP脚本中。 您应该在类型:'POST'之后添加dataType:'json',然后您的成功函数将被调用并将获取服务器在数据中返回的结果