Это мой ajax ():
<script> $(document).ready(function(){ $("form").submit(function(){ var url = "xxx.php/"; var param = $("#streetInput").serialize() + "&"; param += $("#cityInput").serialize() + "&"; param += $("#stateInput").serialize(); $.ajax({ url: url, data: param, type: 'GET', dataType: 'json', success: function(data) { // parse the data here alert(data); }, error: function(){ alert("error"); } }); }); }); </script>
И я успешно передаю параметры в файл xxx.php, а также конвертирую некоторые данные в форму JSON и правильно их повторяю.
<?php $streetArr = array(); $cityArr = array(); $street = $city = $state = ""; $url = 'http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id=myid&'; if($_SERVER["REQUEST_METHOD"] == "GET") { $street = test_input($_GET["streetInput"]); $city = test_input($_GET["cityInput"]); $state = test_input($_GET["stateInput"]); $streetArr = strsToArray($street); $cityArr = strsToArray($city); $url = $url . "address=" . $streetArr[0]; for($x = 1; $x < count($streetArr); $x++) if($streetArr[$x] != "#") $url .= "+$streetArr[$x]"; $url = $url . "&citystatezip=" . $cityArr[0]; for($x = 1; $x < count($cityArr); $x++) $url .= "+$cityArr[$x]"; $url = $url . "%2C+" . $state . "&rentzestimate=true"; $xml = simplexml_load_file($url); echo json_encode($xml); ..... // some functions }
но когда я отлаживаю Firefox с Firebug, я не могу получить «alert (data)», но «alert (« error »)». Я не знаю, почему мой запрос ajax () не имеет успеха.
Я получаю данные JSON на стороне сервера:
{"request":{"address":"2636 Menlo Street","citystatezip":"Los Angeles, CA"},"message":{"text":"Request successfully processed","code":"0"},"response":{"results":{"result":{"zpid":"20593083","links":{"homedetails":"http:\/\/www.zillow.com\/homedetails\/2636-Menlo-Ave-Los-Angeles-CA-90007\/20593083_zpid\/","graphsanddata":"http:\/\/www.zillow.com\/homedetails\/2636-Menlo-Ave-Los-Angeles-CA-90007\/20593083_zpid\/#charts-and-data","mapthishome":"http:\/\/www.zillow.com\/homes\/20593083_zpid\/","comparables":"http:\/\/www.zillow.com\/homes\/comps\/20593083_zpid\/"},"address":{"street":"2636 Menlo Ave","zipcode":"90007","city":"Los Angeles","state":"CA","latitude":"34.03163","longitude":"-118.289972"},"FIPScounty":"6037","useCode":"Duplex","taxAssessmentYear":"2013","taxAssessment":"152082.0","yearBuilt":"1924","lotSizeSqFt":"5242","finishedSqFt":"1728","bathrooms":"2.0","bedrooms":"2","lastSoldDate":"07\/29\/1996","lastSoldPrice":"115000","zestimate":{"amount":"452915","last-updated":"10\/26\/2014","oneWeekChange":{"@attributes":{"deprecated":"true"}},"valueChange":"3507","valuationRange":{"low":"375919","high":"520852"},"percentile":"0"},"rentzestimate":{"amount":"1887","last-updated":"10\/27\/2014","oneWeekChange":{"@attributes":{"deprecated":"true"}},"valueChange":"-9","valuationRange":{"low":"1415","high":"2680"}},"localRealEstate":{"region":{"@attributes":{"id":"268581","type":"neighborhood","name":"West Adams"},"links":{"overview":"http:\/\/www.zillow.com\/local-info\/CA-Los-Angeles\/West-Adams\/r_268581\/","forSaleByOwner":"http:\/\/www.zillow.com\/west-adams-los-angeles-ca\/fsbo\/","forSale":"http:\/\/www.zillow.com\/west-adams-los-angeles-ca\/"}}}}}}}
После нажатия кнопки «Отправить» сначала отображается предупреждение («ошибка»), а затем перенаправляется на xxx.php, как я указал в