本文介绍了我无法在Laravel中使用route :: post(错误419(未知状态))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Ajax代码,它与type ="get"一起工作,但我想将类型更改为"post"
I have a code of Ajax, it works with the type="get" but I want to change the type to "post"
$.ajax({
url: '/crear',
type: 'post',
cache: 'false',
contentType: 'false',
processData: 'false',
data: {'nombre':Vnombre,'codigo':Vcodigo,'descripcion':Vdescripcion,'arrayCategorias':arrayCategoriasAux,'arrayColores':arrayColoresAux,'arrayTallas':arrayTallasAux}
}).done(function(data){
window.location.replace("/almacen/producto");
});
我在web.php中更改了路线
I changed my route in web.php
Route::post('/crear', 'ProductoController@store');
但是它没有用,我的浏览器中出现错误419(状态未知)
But It didn't work, I have the error 419 (unknown status) in my Browser
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="csrf-token" content="{{ csrf_token() }}">
<script src="{{asset('js/jquery-3.2.1.js')}}"></script>
<script type="text/javascript">
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
我的控制器:
public function store(ProductoFormRequest $request)
{
if ($request->ajax()) {
if (!(empty($request->nombre)) && !(empty($request->codigo)) && !(empty($request->descripcion))) {
# code...
$producto = new Producto;
$producto->codigo = $request->codigo;
$producto->nombre = $request->nombre;
$producto->descripcion = $request->descripcion;
$producto->stock = '0';
$producto->estado = '1';
$producto->save();
}
}
}
我的请求
<?php
namespace sistema\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ProductoFormRequest extends FormRequest
{
/**
* 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 rules()
{
return [
'codigo' => 'required|max:45',
'nombre' => 'required|max:45',
'descripcion' => 'max:45',
'stock' => 'max:11',
];
}
}
我只想将文件[]和其他数组[]发送到我的控制器,如果有人有更好的主意,请帮助我
Only i want to send Files [] and others arrays [] to my controller, if anyone has a better idea please help me
我的表格
<form onsubmit="FuncionGuardar();" method="Post" enctype="multipart/form-data" class="form-horizontal" id="add_user_form">
{{csrf_field()}}
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<label for="codigo"> Nombre</label>
<input class="form-control" required value="{{old('nombre')}}" type="text" id="inNombre" name="nombre" placeholder="Nombre">
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<label for="codigo"> Codigo</label>
<input class="form-control" required value="{{old('codigo')}}" type="text" id="inCodigo" name="codigo" placeholder="Codigo">
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<label for="codigo"> Descripcion</label>
<input class="form-control" required value="{{old('descripcion')}}" type="text" id="inDescripcion" name="descripcion" placeholder="Descripcion">
</div>
</div>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<div class="form-group">
<label>Categorias</label>
<div class="input-group">
<select id="CategoriasSelect" name="idt_categoria" class="form-control" >
@foreach($categorias as $cat)
<option value="{{$cat->idt_categoria}}">{{$cat->nombre}}</option>
@endforeach
</select>
<span class="input-group-btn">
<a class="btn btn-primary " href="#" role="button" onclick="CrearBotonCategorias()">Añadir</a>
</span>
</div>
<small class="text-muted" >*Clic en la Categoria para eliminarla</small>
<br>
<div class="alert alert-info" role="alert">
<div class="btn-toolbar" role="toolbar" aria-label="Toolbar with button groups">
<div class="btn-group mr-2" role="group" aria-label="First group" id="DivCategorias">
<!-- Botones de categorias agregados -->
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<div class="form-group">
<label>Colores</label>
<div class="input-group">
<select id="ColoresSelect" name="idt_color" class="form-control" >
@foreach($colores as $coloKey)
<option value="{{$coloKey->idt_color}}" style="background-color:{{$coloKey->color}};">
{{$coloKey->nombre}}
</option>
@endforeach
</select>
<span class="input-group-btn">
<a class="btn btn-primary " href="#" role="button" onclick="CrearBotonColores()">Añadir</a>
</span>
</div>
<small class="text-muted" >*Clic en el Color para eliminarlo</small>
<br>
<div class="alert " role="alert">
<div class="btn-toolbar" role="toolbar" aria-label="Toolbar with button groups">
<div class="btn-group mr-2" role="group" aria-label="First group" id="DivColores">
<!-- Botones de categorias agregados -->
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<div class="form-group">
<label>Tallas</label>
<div class="input-group">
<select id="TallasSelect" name="idt_categoria" class="form-control" >
@foreach($tallas as $tallaKey)
<option value="{{$tallaKey->idt_talla}}">{{$tallaKey->nombre}}</option>
@endforeach
</select>
<span class="input-group-btn">
<a class="btn btn-primary " href="#" role="button" onclick="CrearBotonTallas()">Añadir</a>
</span>
</div>
<small class="text-muted" >*Clic en la Talla para eliminarla</small>
<br>
<div class="alert alert-warning" role="alert">
<div class="btn-toolbar" role="toolbar" aria-label="Toolbar with button groups">
<div class="btn-group mr-2" role="group" aria-label="First group" id="DivTallas">
<!-- Botones de tallas agregados -->
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<div class="form-group">
<label>Imagenes</label>
<div class="input-group">
<input type="file" id="Imagenes" class="filestyle" name="files[]" accept=".jpg, .jpeg, .png" data-btnClass="btn-primary" multiple>
<output id="list"></output>
</div>
<br>
<div class="alert alert-success" role="alert">
<div class="btn-toolbar" role="toolbar" aria-label="Toolbar with button groups">
<div class="btn-group mr-2" role="group" aria-label="First group" id="DivImagenes">
<!-- Botones de categorias agregados -->
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Submit">
<a class="btn btn-danger" href="javascript:window.history.back();" role="button">Cancelar</a>
</div>
</div>
</div>
</form>
推荐答案
示例ajax请求
$('#submit-signup').on("click", function (e) {
e.preventDefault();
var formDataValues = document.forms.namedItem("signup-form");
var formValues = new FormData(formDataValues);
$.ajax({
type: 'POST',
url: siteUrl + '/signup',
processData: false,
contentType: false,
dataType: 'json',
headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
data: formValues,
success: function (data) {
},
error: function (data) {
}
});
});
已更新419错误,因为您尚未在表单中添加令牌将此行添加到表单中
Updated419 error becasue of you have not added token in your formadd this line inside form
{{csrf_field()}}
更新2 添加表单令牌非常简单
Update 2to add form token very simple
<form action="{{url('add-user')}}" method="Post" enctype="multipart/form-data" class="form-horizontal" id="add_user_form">
{{csrf_field()}}
<div class="form-body">
<h3 class="form-section">Person Info</h3>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="control-label col-md-3">First Name</label>
<div class="col-md-9">
<input type="text" name="first_name" id="first_name" class="form-control first_name" placeholder="First Name">
</div>
</div>
</form>
这篇关于我无法在Laravel中使用route :: post(错误419(未知状态))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!