javascript 错误 422 Ajax Post 使用 Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32757586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error 422 Ajax Post using Laravel
提问by Ricki Moore
I'm trying to make a simple Ajax post using Laravel 5. I read that there is a issue with the Csrf Token matching and that i could put my uri into the VerifyCsrfToken expection to step around this. This part functions, however now I get a 422 error when i make the post.
我正在尝试使用 Laravel 5 制作一个简单的 Ajax 帖子。我读到 Csrf 令牌匹配存在问题,我可以将我的 uri 放入 VerifyCsrfToken 期望中来解决这个问题。这部分起作用,但是现在我在发帖时收到 422 错误。
Did I mess something up in my code? How can I get this working? Here is what I have:
我在我的代码中搞砸了什么吗?我怎样才能让它工作?这是我所拥有的:
HTML:
HTML:
<div class = "q-form">
{!!Form::open(array('url' => 'questions')) !!}
<div class = "form-group">
{!! Form::hidden('user_id', $myid, ['class' => 'form-control']) !!}
{!!Form::label('title', 'Title:')!!}
{!!Form::text('title', null, ['class'=> 'form-control'])!!}
{!!Form::label('question', 'Question:')!!}
{!!Form::textarea('question', null, ['class'=> 'form-control area', 'placeholder' => 'What would you like to ask?'])!!}
{!!Form::submit('Ask!', ['class'=> 'btn btn-danger form-control ask'])!!}
</div>
{!! Form::close() !!}
</div>
JS:
JS:
$('.ask').click(function(e) {
e.preventDefault();
var postData = $(this).serializeArray();
var base_url = 'http://rem-edu-es.eu1.frbit.net/';
$.ajax({
type: "POST",
url: base_url + "questions",
data: postData,
success: function (data) {
console.log(data);
}
});
});
Controller:
控制器:
public function book()
{
if(Request::ajax()){
return Response::json(Input::all());
}
}
VerifyCsrfToken:
验证CsrfToken:
class VerifyCsrfToken extends BaseVerifier
{
protected $except = [
'book/*',
'book',
'questions'
];
}
回答by Harry Bosh
Error handling an object within response.
处理响应中的对象时出错。
error :function( data ) {
if( data.status === 422 ) {
var errors = $.parseJSON(data.responseText);
$.each(errors, function (key, value) {
// console.log(key+ " " +value);
$('#response').addClass("alert alert-danger");
if($.isPlainObject(value)) {
$.each(value, function (key, value) {
console.log(key+ " " +value);
$('#response').show().append(value+"<br/>");
});
}else{
$('#response').show().append(value+"<br/>"); //this is my div with messages
}
});
}
回答by Victor Levandovski
422 is a default response when validation fails. When you processing ajax response, you need to process "success" and "error". Example from my code:
422 是验证失败时的默认响应。处理ajax响应时,需要处理“成功”和“错误”。我的代码示例:
$.ajax({
url: $(this).data('url'),
type: "post",
dataType: "json",
data: values,
success: function (data) {
$('#list').append(data.view);
},
error: function (data) {
var errors = $.parseJSON(data.responseText);
$.each(errors, function (key, value) {
$('#' + key).parent().addClass('error');
});
}
});
By the way, you can pass a _tokenparameter with your ajax post, then you don't need to disable CSRF protection. Just add a hidden input
顺便说一句,您可以在 ajax 帖子中传递一个_token参数,然后您就不需要禁用 CSRF 保护。只需添加一个隐藏的输入
{!! Form::token() !!}
in your form that you send to a server via ajax.
在您通过ajax发送到服务器的表单中。
回答by dipesh
Try adding status code in your response:
尝试在响应中添加状态代码:
public function book()
{
if(Request::ajax()){
return Response::json(Input::all(), 200);
}
}