2016-03-02 51 views
2

我有一個問題,可能已經解決或可能沒有解決過,但我似乎是唯一使用純JavaScript而不是JQuery來完成我簡單的AJAX請求。Zillow數據 - json_encode不工作 - 適用於常規變量

首先這裏是我的AJAX:

function getZestimate(address,csz){ 
var xmlhttp = new XMLHttpRequest(); 

    var userdata = "address="+address+"&csz="+csz; 

    xmlhttp.open("POST","../wp-content/themes/realhomes/submit_address.php",true); 

    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

    xmlhttp.onreadystatechange = function(){ 
     if(xmlhttp.readyState == 4 && xmlhttp.status == 200){ 
      retrieve = JSON.parse(xmlhttp.responseText); 
      document.getElementById("zestimateArea").innerHTML = 
       '<div id="zillowWrap"> 
        <div id="logoANDtag"> 
        <a href="http://www.zillow.com"><img src="http://www.zillow.com/widgets/GetVersionedResource.htm?path=/static/logos/Zillowlogo_150x40.gif" width="150" height="40" alt="Zillow Real Estate Search" id="ZillowLogo" /></a> 
        <span id="zestimateTag">Zestimate&reg;</span> 
        </div> 
        <span id="zestimatePrice">'+retrieve[0]+'</span> 
       </div> 
       <div id="zillowDisclaimer"> 
        <span>&copy; Zillow, Inc., 2006-2014. Use is subject to <a href="http://www.zillow.com/corp/Terms.htm">Terms of Use</a></span 
        <span>What&rsquo;s a <a href="http://www.zillow.com/wikipages/What-is-a-Zestimate">Zestimate?</a> 
       </div>'; 
     } 
     else{ 
      document.getElementById("zestimateArea").innerHTML = "Error!" 
     } 
    } 

    xmlhttp.send(userdata); 
    document.getElementById("zestimateArea").innerHTML = "Generating..."; 

    return false; 
} 

接下來,這裏是我的PHP:

<?php 
    $zillow_id = '1234'; 
    $search = $_POST['address']; 
    $citystate = $_POST['csz']; 
    $address = urlencode($search); 
    $citystatezip = urlencode($citystate); 

    $url = "http://www.zillow.com/webservice/GetSearchResults.htm?zws-id=".$zillow_id."&address=".$address."&citystatezip=".$citystatezip; 
    $result = file_get_contents($url); 
    $data = simplexml_load_string($result); 

    $zpidNum = $data->response->results->result[0]->zpid; 

    $zurl = "http://www.zillow.com/webservice/GetZestimate.htm?zws-id=".$zillow_id."&zpid=".$zpidNum; 
    $zresult = file_get_contents($zurl); 
    $zdata = simplexml_load_string($zresult); 

    $zestimate=$zdata->response->zestimate->amount; 
    $street=$zdata->response->address->street; 
    $city=$zdata->response->address->city; 
    $state=$zdata->response->address->state; 
    $zip=$zdata->response->address->zip; 
    $one='one'; 
    $two='two'; 
    header("Content-Type: application/json; charset=utf-8", true); 
    echo json_encode(array($zestimate,$street)); 
?> 

什麼在我的AJAX返回的是[object Object]在我的控制檯中沒有錯誤。

但是,請參閱2變量$one$two?如果我將它們放在json_encode,如echo json_encode(array($one,$two));,它會像它應該那樣返回one。我不確定Zillow數據有什麼不同。我可以echo它個別沒有問題。但我需要發送多個值才能使用。有任何想法嗎?

+1

的SimpleXML節點可能需要轉換爲字符串第一。如果您執行'$ zestimate =(string)$ zdata-> response-> zestimate-> amount,它會起作用嗎? $ street =(string)$ zdata-> response-> address-> street;'?其背後的原因隱藏在手冊[here]中(http://php.net/manual/en/simplexml.examples-basic.php#example-6046)。這是因爲SimpleXML節點是對象,所以'json_encode'不能像你期望的那樣處理它們。 – drew010

+0

@ drew010是的。把它放在答案中,我會標記它。謝謝! – Christine268

回答

1

當您使用SimpleXML解析文檔時,所有節點都是objects,當您試圖回顯它們時會將其轉換爲字符串,但當給予像json_encode這樣的函數時,您不會得到期望的結果。

爲了讓他們字符串,這樣json_encode作品,試試這個:

$zestimate = (string)$zdata->response->zestimate->amount; 
$street = (string)$zdata->response->address->street; 

echo json_encode([$zestimate, $street]);