【发布时间】:2021-07-06 14:55:13
【问题描述】:
大家好,我很绝望,我没有完全理解 Ajax 的这种语法/概念。
我的页面上有 2 个表单。根据隐藏字段的值,我的控制器中的某个逻辑将被执行。其实很简单——我想。我试图将一个数组从我的 ajax 传递给我的控制器,然后将它传递回我的视图。我知道这没有多大意义。但我试图了解它是如何工作的。第一个逻辑被执行,但另一个让我收到此错误:
jqXHR is : [object Object] textStatus is : parsererror errorThrown is : SyntaxError: Unexpected end of JSON input
这是为什么呢?各位大侠能给个建议吗。
更新
我尽量不放整个代码,而只是将其简化为主要问题。但由于它引起了根本原因的混淆,我将展示可能相关部分的整个代码。我将问题编辑为以下内容。
刀片
<center>
<div class="col-md-12">
<h3>xlsx, xls, ods, csv to Text</h3>
<form id="xlsForm" enctype="multipart/form-data">
@csrf
<input type="file" name="excelfile" />
<input name ="loadSubmit" id="loadSubmit" type="submit"/>
<input type ="hidden" name="load" value="0">
</form>
</div>
</center>
<div class ="row">
<div class ="col-md-3">
<div class="container mt-5">
<h2 id="words" class="mb-4">Skills found</h2>
</div>
<form id="xlsFormUpdate" enctype="multipart/form-data">
<input type ="hidden" name="load" value="1">
<div id="inner">
</div>
<input name ="updateSubmit" id="updateSubmit" type="submit"/>
</form>
</div>
<div class ="col-md-9">
@include('layouts.partials.datatable')
</div>
</div>
控制器:
if ($request->input('load') == '0') {
//some code -- this works fine
return response()->json($data); //This data can be send - no problem
} elseif (($request->input('load') == '1')) {
$data = $request->post('checkbox_array');
return response->json($data); // i tried json_encode too .. not working.
}
jQuery 代码
$(document).ready(function(){
$('#xlsForm').submit(function uploadFile(event){
event.preventDefault();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url: "{{route('ExcelToArray')}}",
method: 'POST',
data: new FormData(this),
dataType: 'JSON',
contentType: false,
cache: false,
processData: false,
success:function (response) {
$("#inner").empty();
$.each(response,function(index,value){
$("#inner").append
('<div class="row">' +
'<div class="col-xs-2">'+
'<input type="checkbox" class="'+value+'" value="'+value+'" name="checkbox[]" checked > <label style="font-weight: bold" for="skillChoice_'+index+'">'+value+' </label>' +
'</div>'+
'<div class="col-xs-1">'+
'<input class="'+value+'" type="number" name="weight[]" min="1" max="3" value="1"> '+
'</div>'+
'</div>');
});
}
});
});
$('#xlsFormUpdate').submit(function uploadFile(event) {
event.preventDefault();
var checkbox_array = [];
$('input[type=checkbox]').each(function (index,value) {
if (this.checked)
{
checkbox_array.push(1);
}
else {
checkbox_array.push(0);
}
});
let checkbox_s = JSON.stringify(checkbox_array)
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url: "{{route('ExcelToArray')}}",
method: "POST",
data: {checkbox_array:checkbox_s},
dataType: 'JSON',
contentType: false,
cache: false,
processData: false,
success: function (response) {
alert('The Response is ' + response);
},
error: function (jqXHR, textStatus, errorThrown) {
alert('jqXHR is : ' + jqXHR
+ 'textStatus is : ' + textStatus
+ ' errorThrown is : ' + errorThrown);
},
})
});
});
【问题讨论】:
-
否
$_POST(['checkbox_array']);不是访问 POST 数据的正确方法。但这不是 AJAX 的问题,而是您对 PHP 语法的理解。$_POST是一个数组变量,可以通过括号语法访问,如下所示:$_POST['checkbox_array']。括号主要用于函数或条件。 php.net/manual/de/langref.php -
您只能将标量类型从一种语言传递到另一种语言,而不是复合类型。复合类型需要序列化(通常是字符串化)才能正确传输。因此您需要
response()->json($compoundValue);或response(json_encode($compoundValue)); -
嗨@shaedrich 访问这些特定数据的方法是什么?例如,我知道访问输入我写
$request->input('name')或文件request->file('name)但在这里我不知道。 -
您应该可以通过
$request->checkbox_array;、$request->post('checkbox_array');或$request->input('checkbox_array');访问该值。 -
请更改此行“return response($data);”返回 response()->json($data),而且你正在循环一个不存在的元素“input[type=checkbox]”
标签: javascript php jquery ajax laravel