请有人帮忙!这是我在 Laravel 中遇到的一个疯狂错误。好的,我有一个表单,我正在尝试将 .doc .pdf 和一个图像文件上传到服务器。因此,在我的 html 中,我使用 type 属性将输入欺骗为文件。当我尝试使用 ajax new FormData() 提交表单时,我看到文件在控制台中的 HTTPS 请求 header 中发送,但服务器的响应根本不显示该文件。看看我的 html 中的代码。
<form id="form" method = "POST" enctype="multipart/form-data" action="{{url('/SetUp/Save')}}" >
<div id = "box" class="container dark well dataBox textBox2">
<script type="text/javascript">
num = 1; // only show the first box and hide the others VM keeping the num var for use later
</script>
@for($i = 1; $i <= $amount; $i++)
<div id = {{"panel".$i}} class="panel panel-default">
<div class="panel-heading">
<button type="button" class="btn btn-danger left">Remove
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
</button>
<h3 class="panel-title"><strong> Location {{$i}}</strong></h3>
</div>
<div class=" pBody panel-body">
<div class="row">
<div class="form-group col-lg-6">
<label class = "control-label" label-default="Address" for="Address">Address
</label>
<input type="text" name= "Address[{{$i}}]" class="form-control" placeholder="Address">
</div>
<div class="form-group top col-lg-6">
<label class = "control-label" label-default="City" for="City">City</label>
<input type="text" name = "City[{{$i}}]" class="form-control"
placeholder="City">
</div>
</div>
<div class="row">
<div class="form-group col-lg-6 ">
<label class = "control-label" label-default="Picture" for="Picture">Picture</label>
<input type="file" name = "Picture[{{$i}}]" class= "form-control" id="Picture"
placeholder="Picture">
</div>
Controller
public function saveData(PostRequest $request) {
}
发布请求
namespace App\Http\Requests;
use App\Http\Requests\Request;
use Log;
class PostRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function wantsJson()
{
return true;
}
public function response(array $errors)
{
return response()->json($errors);
}
public function rules() {
$rules = [];
var_dump($this->request); // request object print out
var_dump($this->request->get('Picture'); // return NULL to console
foreach($this->request->get('Picture') as $key => $val) // error 500 meaning Picture is null.
{
$rules['Picture.'.$key] = 'required|image';
}
}
如您所见,我正在尝试验证请求,但最终出现错误。 当我 var_dump 请求时,我得到 null 并且它没有显示在 var_dump 中,这里是 var_dump 打印输出
object(Symfony\Component\HttpFoundation\ParameterBag)#194 (1) {
["parameters":protected]=>
array(15) {
["Address"]=>
array(5) {
[1]=>
string(0) ""
[2]=>
string(0) ""
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
}
["City"]=>
array(5) {
[1]=>
string(0) ""
[2]=>
string(0) ""
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(0) ""
}
}
这是我的 ajax
/////////////send data to server
$("#form").on("submit",function(event) {
console.log("Fire");
$.ajax({
type: 'POST',
url: '/SetUp/Save',
//data: $(this).serialize(), // put the data in url form // send data over
data: new FormData( this ), // send data including file input to xml to process for sever
processData: false,
contentType: false,
dataType: 'json',
success: function(data){
HolderArray = []
JsonKeys = Object.keys(data);
for(x in data){
HolderArray.push(data[x])
}
for (var i = 0; i < JsonKeys.length; i++) {
holder = JsonKeys[i].replace(".","[");
$("[name='"+holder+"]'"+"]").parent().addClass(' animated shake has-error') .append(' <span class="help-block"> <strong>'+HolderArray[i]+'</strong></span>')
}
},
error: function(data){
var errors = data.responseJSON;
console.log(errors);
// Render the errors with js ...
}
});
return false;
});
找不到图片。请求对象似乎没有获取文件输入。我一直在网上搜索并没有解决这个问题。请帮助我,我也是 Laravel 新手,所以请帮助我。谢谢您
最佳答案
啊哈我解决了我的问题!在我的 Post Request 类中而不是调用
$this->request->get('Picture'); // this will return null
// But
$this->file('Picture');// will return the file and show that the file has been uploaded. It seems like the file does not bind the to request object.
现在而不是像这样验证我的字段
foreach($this->request->get('Picture') as $key => $val)
{
$rules['Picture.'.$key] = 'required';
} // this gives a null error
我把它改成这个并且它有效
foreach($this->file('Picture') as $key => $val)
{
$rules['Picture.'.$key] = 'required';
}
它有效! 。
感谢您的帮助!!!
关于javascript - Laravel 5.2 请求返回 null AJAX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37691285/