【发布时间】:2020-06-16 11:12:26
【问题描述】:
当我发送一个变量并在将变量转换为字符串后与另一个变量连接时,此错误会显示“TypeError: invalid 'in'operand a”。我不明白为什么我的 jquery 代码如下:
$(document).on('change', '.AccountType', function () {
var MemberId = document.getElementById("MemberId").value;
var AccountType = document.getElementById("AccountType").value;
var acno = MemberId + AccountType;
$.ajax({
type: 'get',
url: 'getAccountno',
data: {'id': acno},
success: function (data) {
$.each(data, function (index, data) {
$('#AccountNo').empty();
document.getElementById("AccountNo").value = data;
});
},
error: function () {
alert("Something happening unexpected, Error!");
}
});
});
我的控制器如下:
public function getAccountno(Request $request){
if($i<=9){ $i = '0'.$i};
$AccountNo = $request->id;
$AccountNo = (string)$AccountNo . (string)$i;
$i++;
$data = DB::table('accountopens')
->select('AccountNo')
->where('AccountNo', $AccountNo)
->get();
if($data == null){
return response()->json($AccountNo);
}
else{
$i++;
return response()->json((string)$AccountNo . (string)$i);
}
}
请描述错误...
【问题讨论】:
-
发生此错误的确切行是什么?是JS还是PHP?
-
这发生在 php laravel 控制器中。确切的行可能在 'getAccountno () 函数第 3 行和第 14 行。@David Rodrigues
-
错误在 js 文件上,我使用了 $.each() ,因为响应不是数组,这就是错误发生的原因。
-
为什么首先需要一个
each循环来处理字符串?