[英]How to get data from API with ajax on select (dropdown option value) in Codeigniter?
I have a case in my code, the result does not appear or work, the city form select is still blank.我的代码中有一个案例,结果没有出现或工作,城市表格选择仍然是空白的。 Here is the HTML:
这是 HTML:
<div class="form-group">
<label>Province</label>
<select class="form-control" name="province" id="province">
<?php foreach($state_data->data as $sd){ ?>
<option id="<?php echo $sd->code; ?>" value="<?php echo $sd->code; ?>" <?php echo (isset($this->session->userdata))?(($this->session->userdata('session_state_code') == $sd->code)?"selected":""):"";?>>
<?php echo $sd->name ;?>
</option>
<?php } ?>
</select>
</div>
<div class="form-group" id="city_section">
<label>City</label>
<select class="form-control" name="city" id="city">
<?php foreach($city_data->data as $cd){ ?>
<option value="<?php echo $cd->code; ?>" <?php echo (isset($this->session->userdata))?(($this->session->userdata('session_city_code') == $cd->code)?"selected":""):"";?>>
<?php echo $cd->name ;?>
</option>
<?php } ?>
</select>
</div>
This the javascript to get data from the controller:这是从控制器获取数据的javascript:
$('#province').on('change', function() {
var id = $(this).val();;
$.ajax({
type: "POST",
dataType: 'json',
url: "<?php echo base_url() ?>edit_profile/get_city",
data: {
id: $(this).val()
},
success: function(data) {
$('select#city').html('');
for (var i = 0; i < data.length; i++) {
$("<option />").val(data[i].code)
.text(data[i].name)
.appendTo($('select#city'));
}
}
});
}
and this the controller to get data from API:这是从 API 获取数据的控制器:
public function get_city(){
$m_url = URL_API;
$function = "get_location";
$i = $_REQUEST['id'];
$url = $m_url . "/" . $function . "?" . "t=city" . "&" . "i=".$i;
$json_data = file_get_contents($url);
$data = json_decode($json_data);
}
Your function does't return anything to ajax you need to echo
json_decode您的函数不会向 ajax 返回任何内容,您需要
echo
显 json_decode
public function get_city(){
$m_url = URL_API;
$function = "get_location";
$i = $_REQUEST['id'];
$url = $m_url . "/" . $function . "?" . "t=city" . "&" . "i=".$i;
$json_data = file_get_contents($url);
echo $data = json_decode($json_data);// use echo here
}
You dont need json_decode(), just print $json_data.您不需要 json_decode(),只需打印 $json_data。 Because, it's already returns as string by file_get_contents().
因为,它已经通过 file_get_contents() 以字符串形式返回。
public function get_city(){
$m_url = URL_API;
$function = "get_location";
$i = $_REQUEST['id'];
$url = $m_url . "/" . $function . "?" . "t=city" . "&" . "i=".$i;
$json_data = file_get_contents($url);
echo $json_data;
}
Convert returned data to JSON object.将返回的数据转换为 JSON 对象。 And then use it:
然后使用它:
success: function(data) {
var jsonObj = JSON.parse(data);
$('select#city').html('');
for (var i = 0; i < jsonObj.length; i++) {
$("<option />").val(jsonObj[i].code)
.text(jsonObj[i].name)
.appendTo($('select#city'));
}
}
Having echo $json_data;
有
echo $json_data;
,now try to put error
callback in your Ajax so that you will see the error response of your request. ,现在尝试将
error
回调放在您的 Ajax 中,以便您将看到您的请求的错误响应。 From there you will have an idea what went wrong within the process.从那里你会知道这个过程中出了什么问题。
error: function(err){
console.log(err.responseText);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.