【发布时间】:2018-03-01 09:55:39
【问题描述】:
更改功能后我得到警报值,但成功功能后,我没有得到任何值
我的 ajax 页面
$(document).ready(function(){
$("#customer").change(function() {
var customer_type = $(this).find(":selected").val();
var dataString = 'customer_type='+ customer_type;
$.ajax({
url: 'http://localhost/capms_v3/ajax/getcust_type.php',
dataType: "json",
data: dataString,
cache: false,
success: function(customerData) {
alert(data);
alert("test");
if(customerData) {
var customer = [customerData];
customerData.forEach(function(item) {
var data = '<tr>';
data+= '<td colspan="4"> </td>';
data+= '<td align="right">'+item.company_id+'</td>';
data+= '<td align="right">'+item.company_name+'</td>';
data+='</tr>';
$('.appendData').append(data);
});
} else {
$("#heading").hide();
$("#records").hide();
$("#no_records").show();
}
}
});
});
});
我的数组值没有在成功函数之后出现,但在 getcusttype 页面值出现在数组中
getcusttype.php
<?php
//header("Content-type:application/json");
include 'db.php';
$db=DbConnect();
if($_REQUEST['customer_type']) {
$sql = "SELECT company_id,company_name FROM ca_customer WHERE customer_type ='".$_REQUEST['customer_type']."'";
$result = mysql_query($sql) or die(mysql_error());
$data = array();
while( $rows = mysql_fetch_array($result) ) {
$data[] = $rows;
}
echo json_encode($data);
} else {
echo 0;
}?>
//var customer =[{"0":"1","company_id":"1","1":"Win Win
web","company_name":"Win Win web"},{"0":"7","company_id":"7","1":"New
Company","company_name":"New Company"},
{"0":"10","company_id":"10","1":"Murugan Super
Store","company_name":"Murugan Super Store"}];
成功后:function(customerdata) if I alert(data) values are getting alert 我不知道我犯了什么错误。
查看
<select id="customer" name="customer_type" class="form-control">
<option value="">Select Customer Type</option>
<?php
foreach($all_ca_customer_type as $ca_customer_type)
{
$selected = ($ca_customer_type['customer_type_id'] == $this->input->post('customer_type')) ? ' selected="selected"' : "";
echo '<option value="'.$ca_customer_type['customer_type_id'].'" '.$selected.'>'.$ca_customer_type['customer_type_name'].'</option>';
}
?>
</select>
<tbody class="appendData">
</tbody>
成功函数后没有得到值。如果有人遇到这个问题,请帮助我。提前谢谢
【问题讨论】:
-
alert(data);没有data变量 - 试试alert(customerData); -
如果您没有收到任何警报,那么您就没有到达
success:- 添加一个error:处理程序并检查错误消息 -
在
getcusttype.php中取消注释header("Content-type:application/json"); -
您在
error:处理程序中收到什么错误消息?
标签: javascript php jquery mysql ajax