2016-09-22 133 views
0

我想发送一个链接上的整个JSON数组,所以我必须发送整个JSON数组右移?因为在格式中它们给了我们一个json_array格式。 下面是Rightmove的的JSON数组,传递JSON数据在AJAX请求

JSON数组,

{ 
    "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": "[email protected]", 
      "auto_email_when_live": true, 
      "auto_email_updates": true 
     } 
    } 
} 

我的每一件事每一个它的参数。 现在,rightmove api使用post方法和url是, https://adfapi.rightmove.co.uk/v1/property/sendpropertydetails 所以我试图发送json数据到rightmove,但它显示了我的错误。我已将整个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错误。

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

+1

那么它可能是有用的知道_What错误你ar e getting_ – RiggsFolly

+0

@RiggsFolly。我在这个特定的网址上得到了回应失败。 –

+0

@RiggsFolly如果我尝试使用本地主机的网址它的工作,但如果我尝试与其他网址的手段跨平台网址它不工作。 –

回答

1

我假设你的JavaScript每次产生在你的PHP脚本为你动态生成的网页!

如果您$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); ?> }, 
+0

谢谢你的回应,它的工作完美。但是,如果你有时间,你可以改变我的ajax脚本禁止我已经编辑了我的问题,你可以请看看吗? –

+0

403基本上意思是“你好,你找到我了,但你不能做你要做的事”。我不知道该API,但作为第一个猜测也许它应该是'data:<?php echo $ json_array; ?>,' – RiggsFolly

2

的问题是, “的print_r” 的Funktion心不是创建一个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); ?> }, 
+0

感谢您回复失败的回复。 –

+0

如果我尝试使用本地主机的网址它的工作,但如果我尝试与其他网址的手段跨平台网址它不工作。 –

+0

您是否检查过您的网页浏览器控制台是否显示任何错误? – Lag