gpt4 book ai didi

javascript - Zillow Data - json_encode 不工作 - 适用于常规变量

转载 作者:行者123 更新时间:2023-11-30 00:12:50 28 4
gpt4 key购买 nike

我有一个问题以前可能已经解决也可能没有解决,但我似乎是这里唯一使用纯 JavaScript 而不是 JQuery 来完成我简单的 AJAX 请求的人。

首先这是我的 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 没问题。但我需要发送多个值才能使用。有什么想法吗?

最佳答案

当您使用 SimpleXML 解析文档时,所有节点都是 objects当您尝试回显它们时,它们会被强制转换为字符串,但是当提供给 json_encode 之类的函数时,您得不到预期的结果。

要使它们成为字符串以便 json_encode 工作,试试这个:

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

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

关于javascript - Zillow Data - json_encode 不工作 - 适用于常规变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35736333/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com