在ajax请求中传递Json数据

时间:2016-09-22 13:09:01

标签: php arrays json ajax

我想在链接上发送整个json数组,所以我必须在右移发送整个json数组?因为在它的格式中,它们为我们提供了json_array格式。 下面是rightmove的json数组,

Json Array,

{
    "network":{
        "network_id": 5
    },
    "branch":{
        "branch_id": 1566,
        "channel": 1,
        "overseas": false
    },
    "property":{
        "agent_ref": "02072013_0406",
        "published": true,
        "property_type": 2,
        "status": 1,
        "new_home": false,
        "student_property": false,
        "create_date": "02-07-2013 00:00:00",
        "update_date": "02-07-2013 00:00:00",
        "date_available": "02-07-2013 00:00:00",
        "contract_months": 12,
        "minimum_term": 12,
        "let_type": 1,
        "address":{
            "house_name_number": "33",
            "address_2": "Rightmove",
            "address_3": "4th Floor",
            "address_4": "Soho Square",
            "town": "London",
            "postcode_1": "W1D",
            "postcode_2": "3QU",
            "display_address": "Soho Square",
            "latitude": 51.514899,
            "longitude": -0.132587,
            "pov_latitude": 51.51482,
            "pov_longitude": -0.13249,
            "pov_pitch": -16.78,
            "pov_heading": 235.75,
            "pov_zoom": 0
        },
        "price_information":{
            "price": 1500,
            "price_qualifier": 0,
            "deposit": 1000,
            "administration_fee": "100",
            "rent_frequency": 1,
            "tenure_type": 1,
            "auction": false,
            "tenure_unexpired_years": 999,
            "price_per_unit_area": 10
        },
        "details":{
            "summary": "Rightmove Test Property",
            "description": "Testing full property schema with all the fields in the call for JSON",
            "features": [
                "Has own Drive",
                "Garage included",
                "Double Glazed"
            ],
            "bedrooms": 2,
            "bathrooms": 2,
            "reception_rooms": 1,
            "parking": [13],
            "outside_space": [29],
            "year_built": 999,
            "internal_area": 100,
            "internal_area_unit": 1,
            "land_area": 100,
            "land_area_unit": 1,
            "floors": 5,
            "entrance_floor": 1,
            "condition": 1,
            "accessibility": [42],
            "heating": [1],
            "furnished_type": 0,
            "pets_allowed": true,
            "smokers_considered": true,
            "housing_benefit_considered": true,
            "sharers_considered": true,
            "burglar_alarm": true,
            "washing_machine": true,
            "dishwasher": true,
            "all_bills_inc": true,
            "water_bill_inc": true,
            "gas_bill_inc": true,
            "electricity_bill_inc": true,
            "tv_licence_inc": true,
            "sat_cable_tv_bill_inc": true,
            "internet_bill_inc": true,
            "business_for_sale": true,
            "comm_use_class":[1,4],
            "rooms": [ {
                "room_name": "room1",
                "room_description": "room1" ,
                "room_length": 10.10,
                "room_width": 20.20,
                "room_dimension_unit": 5,
                "room_photo_urls": ["http://www.rightmove.com/image1.JPG"]
            } ]
        },
        "media": [ {
            "media_type":1,
            "media_url":"www.rightmove.com/image1.JPG",
            "caption":"This is an image",
            "sort_order":1,
            "media_update_date": "02-07-2013 12:12:12"
        } ],
        "principal": {
            "principal_email_address": "principal@rightmove.co.uk",
            "auto_email_when_live": true,
            "auto_email_updates": true
        }
    }
}

我得到了每一个参数。 现在,右移api使用post方法和url, https://adfapi.rightmove.co.uk/v1/property/sendpropertydetails 所以我试图将json数据发送到右移,但它显示我错误。我已将整个json Array存储到$json_array

 jQuery.ajax({
                        url: 'https://adfapi.rightmove.co.uk/v1/property/sendpropertydetails/',           
                        type : "POST",
                        crossDomain: true,
                        dataType: "jsonp",
                        data: {data : <?php echo $json_array; ?>},
                        success: function(data) {
                              alert(data);
                        }
            });
        });

我也在标题中添加了但它显示403错误。

编辑错误:它显示响应失败但在localhost中我在本地文件中得到了响应,所以我认为ajax调用正常工作。

2 个答案:

答案 0 :(得分:2)

问题是“print_r”功能不会创建一个JSON-Objekt 正如php.net

所述
<?php
       $a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x','y', 'z'));
       print_r ($a);
?>

会创建

Array
 (
[a] => apple
[b] => banana
[c] => Array
    (
        [0] => x
        [1] => y
        [2] => z
    )
)

解决方案是使用json_encode而不是print_r

所以你只需要改变

data : {data :<?php print_r($json_array); ?> }

data : {data :<?php json_encode($json_array); ?> },

答案 1 :(得分:2)

我假设您每次在PHP脚本中生成javascript,因为您动态构建网页!

如果您的$json_array变量已经是JSON字符串,就像上面显示的JOSNString一样,这是错误的

data : {data :<?php print_r($json_array); ?> },

应该是

data : {data :<?php echo $json_array; ?> },

如果数据保存在php数组中,即不是JSONString,那么你应该

data : {data :<?php json_encode($json_array); ?> },