In this code I have a normal dropdown where I have some city name. Now, What I actually want when I change any city then through jquery I want to get json
data in my alert box which is not working yet. I don't know why? So, How can I do this?
在这段代码中我有一个正常的下拉列表,我有一些城市名称。现在,当我改变任何城市然后通过jquery时我真正想要的是我想在我的警报框中获取json数据但尚未使用。我不知道为什么?那么,我该怎么办呢?
$(document).ready(function() {
$("#city").change(function() {
name = $(this).val();
console.log(name);
$.ajax({
type: "POST",
dataType: "json",
data: {
"name": name
},
url: "http://postalpincode.in/api/postoffice/" + name,
success: function(data) {
console.log(data);
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="city" id="city">
<option value="">Select City</option>
<option value="delhi">delhi</option>
<option value="ghaziabad">ghaziabad</option>
<option value="noida">noida</option>
<option value="meerut">meerut</option>
</select>
1 个解决方案
#1
4
Please check below code
请检查以下代码
<select name="city" id="city">
<option value="">Select City</option>
<option value="delhi">delhi</option>
<option value="ghaziabad">ghaziabad</option>
<option value="noida">noida</option>
<option value="meerut">meerut</option>
</select>
Ajax Called
<script>
$(document).ready(function(){
$("#city").change(function(){
name = $(this).val();
/*For PHP called is for Cross-Origin Request Blocked*/
$.ajax({
type:"GET",
dataType: "json",
data:{name: name},
url:"test.php",
success:function(data)
{
alert('Get Success');
console.log(data);
}
});
});
});
</script>
PHP file code for Cross-Origin Request Blocked -
阻止跨源请求的PHP文件代码 -
<?php
$name_city = rawurlencode($_GET['name']);
$url = "http://postalpincode.in/api/postoffice/".$name_city;
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,"$url");
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_HEADER, false);
$postoffice_data = curl_exec($curl_handle);
curl_close($curl_handle);
$postoffice_data = json_decode($postoffice_data);
echo json_encode($postoffice_data);
exit;
?>
#1
4
Please check below code
请检查以下代码
<select name="city" id="city">
<option value="">Select City</option>
<option value="delhi">delhi</option>
<option value="ghaziabad">ghaziabad</option>
<option value="noida">noida</option>
<option value="meerut">meerut</option>
</select>
Ajax Called
<script>
$(document).ready(function(){
$("#city").change(function(){
name = $(this).val();
/*For PHP called is for Cross-Origin Request Blocked*/
$.ajax({
type:"GET",
dataType: "json",
data:{name: name},
url:"test.php",
success:function(data)
{
alert('Get Success');
console.log(data);
}
});
});
});
</script>
PHP file code for Cross-Origin Request Blocked -
阻止跨源请求的PHP文件代码 -
<?php
$name_city = rawurlencode($_GET['name']);
$url = "http://postalpincode.in/api/postoffice/".$name_city;
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,"$url");
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_HEADER, false);
$postoffice_data = curl_exec($curl_handle);
curl_close($curl_handle);
$postoffice_data = json_decode($postoffice_data);
echo json_encode($postoffice_data);
exit;
?>