我在更改功能后获得警报值,但在成功功能之后,我没有得到任何值
我的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)如果我发出警报(数据)值警报我不知道我犯了什么错误。
视图
<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>
在成功函数之后没有得到值。如果有人面对这个问题,请帮助我。提前谢谢
答案 0 :(得分:0)
在header("Content-type:application/json");
getcusttype.php
答案 1 :(得分:0)
您的alert()
函数似乎正在引用data
数据来自何处?尝试使用alert(customerData)
代替alert(data)
。
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();
}
}
如果您希望提醒您附加行,则应在alert(data)
调用forEach()
变量后将data
调用移至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>';
alert(data);
$('.appendData').append(data);
});
块。
例如:
function num() {
var number = prompt("Enter number");
document.getElementById("number").innerHTML = number;
}