Javascript 如何使用Ajax将PHP文件作为数组获取响应?

Javascript 如何使用Ajax将PHP文件作为数组获取响应?,javascript,php,json,ajax,Javascript,Php,Json,Ajax,我试图通过在HTML格式的文本框中输入邮政编码来获取完整地址。我有两个文件,第一个是ajax函数,第二个是PHP代码。我不确定我的ajax代码是否向PHP发送请求,有人能帮我吗 以下是ajax文件: <script type="text/javascript"> $(document).ready(function(){ $('.addressbutton').click(function(){ ss= document.getElementById("address"

我试图通过在HTML格式的文本框中输入邮政编码来获取完整地址。我有两个文件,第一个是ajax函数,第二个是PHP代码。我不确定我的ajax代码是否向PHP发送请求,有人能帮我吗

以下是ajax文件:

<script type="text/javascript">
$(document).ready(function(){
  $('.addressbutton').click(function(){
    ss=  document.getElementById("address").value;
    alert(ss);
    $.ajax({
      url: 'findaddress.php',
      type: 'post',
      data: ss,
      success: function(response){
        var replay = response.postal_code;
        alert(replay);
        document.getElementById('address').innerHTML = response.postal_code;
        document.getElementById('address2').innerHTML = response.route;
        document.getElementById('address3').innerHTML = response.locality;
        document.getElementById('address4').innerHTML = response.postal_town;
        document.getElementById('address5').innerHTML = response.administrative_area_level_2;
      }
    });
    return false;
  });
});
</script>

$(文档).ready(函数(){
$('.addressbutton')。单击(函数(){
ss=document.getElementById(“地址”).value;
警觉(ss);
$.ajax({
url:'findaddress.php',
键入:“post”,
资料来源:ss,
成功:功能(响应){
var replay=response.postalu代码;
警报(重播);
document.getElementById('address').innerHTML=response.postal_code;
document.getElementById('address2')。innerHTML=response.route;
document.getElementById('address3')。innerHTML=response.locality;
document.getElementById('address4')。innerHTML=response.postall\u town;
document.getElementById('address5')。innerHTML=response.administration\u area\u level\u 2;
}
});
返回false;
});
});
下面是PHP代码(findaddress.PHP)

状态!=“好的”){
echo“出现问题:”.$parsedXML->状态;
}
$myAddress=array();
foreach($parsedXML->result->address\u组件作为$component){
if(是_数组($component->type)){
$type=(字符串)$component->type[0];
}否则{
$type=(字符串)$component->type;
}
$myAddress[$type]=(字符串)$component->long\u name;
}
$f1=$myAddress['邮政编码'];
$f2=$myAddress['route'];
$f3=$myAddress['Location'];
$f4=$myAddress['postal_town'];
$f5=$myAddress['Administration_area_level_2'];
$f6=$myAddress['country'];
//打印(我的地址);
$ORegistation=数组(
“邮政编码”=>$f1,
“路线”=>f2美元,
“地点”=>f3美元,
“邮政镇”=>f4美元,
“行政区域级别2”=>f5美元,
“国家”=>f6美元
);
$account\u json=json\u encode($oregisteration);
echo$account_json;
?>
HTML

<form name="frmRegistration" id="signup-form" method="post">
    <div>
        <input type="text" name="address" id="address" class="findaddress"  placeholder="Postal code"/>
        <input type="button" name="addressbutton" class="addressbutton" value="Find" id="findaddress" />
        <input type="text" name="address2" id="address2" class="findaddress"  placeholder="Line 1"/>
        <input type="text" name="address3" id="address3" class="findaddress"  placeholder="Line 2"/>
        <input type="text" name="address4" id="address4" class="findaddress"  placeholder="Line 3"/>
        <input type="text" name="address5" id="address5" class="findaddress"  placeholder="Line 4"/>
    </div>
 </form>
在脚本中添加了有关所做更改的注释

PHP文件(findaddress.PHP)

状态!=“好的”){
echo“出现问题:”.$parsedXML->状态;
}
$myAddress=array();
foreach($parsedXML->result->address\u组件作为$component){
如果(是数组($component->type))$type=(字符串)$component->type[0];
else$type=(字符串)$component->type;
$myAddress[$type]=(字符串)$component->long\u name;
}
echo json_编码($myAddress);
模具();
?>

再次删除不相关的索引和不相关的语句。

您没有正确发送数据。。 如果您想在php中获得地址的值,这是来自ajax的post,请执行以下操作

数据:{地址:ss},
// 或者在ajax中添加
dataType:'json'
,或者使用
jsonParse(response)


您的响应中有一个字符串您不能直接使用
response.postal\u code

有一个带有html表单的ajax代码只是为了更好地理解它

 <form name="frmRegistration" id="signup-form" method="post">

    <div><input type="text" name="address" id="address" class="findaddress"  placeholder="Postal code"/>
        <input type="button" name="addressbutton" class="addressbutton" value="Find" id="findaddress" onclick="javascript:hello()"/>
        <input type="text" name="address2" id="address2" class="findaddress"  placeholder="Line 1"/>
        <input type="text" name="address3" id="address3" class="findaddress"  placeholder="Line 2"/>
        <input type="text" name="address4" id="address4" class="findaddress"  placeholder="Line 3"/>
        <input type="text" name="address5" id="address5" class="findaddress"  placeholder="Line 4"/>
    </div>


    <script type="text/javascript">
     $(document).ready(function(){
         $('.addressbutton').click(function(){
             ss =  document.getElementById("address").value;
             //alert(ss);
             $.ajax({
                 url: 'findaddress.php',
                 type: 'post',
                 data: {address:ss},
                 success: function(response){
                     var replay = response.postal_code;
                     alert(replay);
                     document.getElementById('address').innerHTML = response.postal_code;
                     document.getElementById('address2').innerHTML = response.route;
                     document.getElementById('address3').innerHTML = response.locality;
                     document.getElementById('address4').innerHTML = response.postal_town;
                     document.getElementById('address5').innerHTML = response.administrative_area_level_2;
                 }
             });
             return false;
         }); //added closing brace and bracket
     });
    </script>
</form>

$(文档).ready(函数(){
$('.addressbutton')。单击(函数(){
ss=document.getElementById(“地址”).value;
//警觉(ss);
$.ajax({
url:'findaddress.php',
键入:“post”,
数据:{地址:ss},
成功:功能(响应){
var replay=response.postalu代码;
警报(重播);
document.getElementById('address').innerHTML=response.postal_code;
document.getElementById('address2')。innerHTML=response.route;
document.getElementById('address3')。innerHTML=response.locality;
document.getElementById('address4')。innerHTML=response.postall\u town;
document.getElementById('address5')。innerHTML=response.administration\u area\u level\u 2;
}
});
返回false;
});//添加了右括号和方括号
});

在这种情况下,您需要确保定义来自服务器的响应类型。我喜欢在$.ajax调用中放置数据类型:'json'。然后在PHP代码中,确保添加类型为application/json的头。这将对某些浏览器产生影响。我喜欢阅读谷歌浏览器的回应预览。它将自动解析响应;特别有助于调试

header('Content-type: application/json');
echo json_encode($account_json);
exit;

那么,您的ajax结果是什么?控制台中是否有任何错误?它是否发出警报(重播)
?当查看开发人员工具中的“网络”选项卡时,是否有POST请求?我试图通过使用警报了解响应是什么,但是它没有运行警报,这意味着在控制台中@AdamKonieskait给我的响应中有一个错误'uncaughttypeerror:form.btnAdd不是一个函数jquery.equalheights.js:17 uncaughttypeerror:$(…).equalHeights不是函数注册。php:429 Uncaught ReferenceError:hello未定义jquery.equalHeights.js:17 Uncaught TypeError:$(…)。equalHeights不是函数9jquery.equalHeights.js:17 Uncaught TypeError:$(…)。equalHeights不是函数“我尝试过这种方法,但不起作用,因此,我建议向您展示我的HTML代码,以便为您的答案@ameen提供更好的想法,但它给了我未定义的响应我正在搜索该答案(lu63ef)@ameenulla0007检查此url,从这里您尝试解析xml,那里没有称为locality的索引。因为你得到的是未定义的
响应。locality
。我认为问题不在于邮政编码,因为我已将其更改为另一个邮政编码,它工作得很好,同时我更改了数组,只将一个值发送到ajax,但它仍然显示undefined@ameenven添加一个示例要搜索什么?
<?php
header('Content-Type: application/json');
$ss         = $_POST['address'];
$postcode   = urlencode($ss);
$url        = 'http://maps.googleapis.com/maps/api/geocode/xml?address='.$postcode.'&sensor=false';
$parsedXML  = simplexml_load_file($url);

if($parsedXML->status != "OK") {
    echo "There has been a problem: " . $parsedXML->status;
}

$myAddress = array();
foreach($parsedXML->result->address_component as $component) {
    if(is_array($component->type)) $type = (string)$component->type[0];
    else $type = (string)$component->type;
    $myAddress[$type] = (string)$component->long_name;
}

echo json_encode($myAddress);
die();
?>
 <form name="frmRegistration" id="signup-form" method="post">

    <div><input type="text" name="address" id="address" class="findaddress"  placeholder="Postal code"/>
        <input type="button" name="addressbutton" class="addressbutton" value="Find" id="findaddress" onclick="javascript:hello()"/>
        <input type="text" name="address2" id="address2" class="findaddress"  placeholder="Line 1"/>
        <input type="text" name="address3" id="address3" class="findaddress"  placeholder="Line 2"/>
        <input type="text" name="address4" id="address4" class="findaddress"  placeholder="Line 3"/>
        <input type="text" name="address5" id="address5" class="findaddress"  placeholder="Line 4"/>
    </div>


    <script type="text/javascript">
     $(document).ready(function(){
         $('.addressbutton').click(function(){
             ss =  document.getElementById("address").value;
             //alert(ss);
             $.ajax({
                 url: 'findaddress.php',
                 type: 'post',
                 data: {address:ss},
                 success: function(response){
                     var replay = response.postal_code;
                     alert(replay);
                     document.getElementById('address').innerHTML = response.postal_code;
                     document.getElementById('address2').innerHTML = response.route;
                     document.getElementById('address3').innerHTML = response.locality;
                     document.getElementById('address4').innerHTML = response.postal_town;
                     document.getElementById('address5').innerHTML = response.administrative_area_level_2;
                 }
             });
             return false;
         }); //added closing brace and bracket
     });
    </script>
</form>
header('Content-type: application/json');
echo json_encode($account_json);
exit;