我编写了以下代码,我面临
“500(内部服务器错误)jquery.js:4”
错误
$(document).ready(function(){
$('#vehicle_no').blur(function(){
$.ajax({
type: "post",
url: '/autolube/Invoices/fetchData',
data: {value: $('#vehicle_no').val()},
dataType: "json",
success: function(data) {
$('#name').val(data.Name);
$('#address').val(data.Address);
$('#promotions').val(data.Promotion);
$('#incentives').val(data.Incentive);
}
});
});
});
而 fetchDaya() 是
public function fetchData(){
if ($this->request->is('post')) {
$this->layout = 'ajax';
$this->autoRender = false;
$user= $this->Invoice->User->findByVehicleNo($_POST['value']);
if($user)
{
$name = $user['User']['first_name'];
$name = $name." ".$user['User']['last_name'];
$address = $user['User']['address'];
$promotion = $this->Invoice->Promotion->findById($user['User']['promotion_id']);
$incentive = $this->Invoice->Incentive->findById($user['User']['incentive_id']);
$data= array('Name'=>$name, 'Address' => $address, 'Promotion' =>$promotion, 'Incentive' =>$incentive);
echo json_encode($data);
}
}
}
如果我进行以下更改,效果很好
$(document).ready(function(){
$('#vehicle_no').blur(function(){
$.ajax({
type: "post",
url: '/autolube/Invoices/fetchData',
data: {value: $('#vehicle_no').val()},
success: function(data) {
alert(data);
}
});
});
});
和
public function fetchData(){
if ($this->request->is('post')) {
$this->layout = 'ajax';
$this->autoRender = false;
$user= $this->Invoice->User->findByVehicleNo($_POST['value']);
if($user)
{
$name = $user['User']['first_name'];
$name = $name." ".$user['User']['last_name'];
echo $name;
}
}
}
我无法找到此问题的解决方案。
最佳答案
由于我的声誉,我无法添加评论,所以就这样吧。我相信这是您的 fetchData() 函数的问题,因为我是内部服务器错误。尝试测试此功能,特别是在:
$promotion = $this->Invoice->Promotion->findById($user['User']['promotion_id']);
$incentive = $this->Invoice->Incentive->findById($user['User']['incentive_id']);
并确保它不会在那里崩溃。
关于javascript - 当我尝试将 JSON 编码数组发送到 jquery 时,发生 500 内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24538219/