在 Laravel 中使用 vue.js 时,在 $.ajax 中获得成功或完成响应时出现问题
Problems getting success or done response in $.ajax when using vue.js in Laravel
我有一个简单的表单,用户应该只在文本区域内键入文本。当用户单击 javascript 内的提交按钮时,我使用 vue 和 ajax 进行调用以将该文本插入数据库。
我的问题是,在用户点击提交后,我希望清除文本区域,但是在将文本保存到数据库后,文本仍然存在。它持续存在。由于我正在使用 vue.js 和 ajax,因此我正在等待这个 .done(成功)回调函数以继续清除表单。
或者如果文本已保存到数据库中,是否还有其他清除文本区域的想法?
这是我的 blade 形式的代码:
<div class="row" id="timeline">
<div class="col-md-4">
<form action="#" v-on:submit="postStatus">{{-- Name of the method in Vue.js --}}
<div class="form-group">
<textarea class="form-control" rows="5" maxlength="140" autofocus placeholder="What are you upto?" required v-model="post" id="textareapost"></textarea>
</div>
<input type="submit" value="Post" class="form-control btn btn-info">
{{ csrf_field() }}
</form>
</div>
<div class="col-md-8">
Timeline
</div>
</div>
这是我的控制器:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use App\Http\Requests;
class PostController extends Controller
{
public function create(Request $request, Post $post)
{
//$request->body;
//Remember, all we really need is the body. Because if create this post through
//the User model, it'll automatically assign us a user_id
//validation:
$this->validate($request,[
'body' => 'required|max:140',
]);
//die('Posted in the controller!: '.$request->body); //$request->get('body');
//creating the post from the user, from the post that belongs to the user:
$createdPost = $request->user()->posts()->create([
'body' => $request->body,
]);
//Now respond with the post that has been created:
return response()->json($post->with('user')->find($createdPost->id));
}
}
到目前为止 javascript 代码:
var csrf_token = $('meta[name="csrf-token"]').attr('content');
/*Event handling within vue*/
//when we actually submit the form, we want to catch the action
new Vue({
el : '#timeline',
data : {
post : '',
token : csrf_token,
},
methods : {
postStatus : function (e) {
e.preventDefault();
console.log('Posted: '+this.post+ '. Token: '+this.token);
var request = $.ajax({
url : '/posts',
method : "POST",
dataType : 'json',
data : {
'body' : this.post,
'_token': this.token,
}
}).done(function (msg) {
console.log('Data saved: '+msg);
this.post = '';/*Attempting to clear the form*/
}.bind(this));/*
request.done(function( msg ) {
console.log('Data saved: '+msg+'. Outside ...');
//$( "#log" ).html( msg );
});
request.fail(function( jqXHR, textStatus ) {
console.log( "Request failed: " + textStatus );
});
}
},
});
保存文本后,我从未收到 .done 部分的 console.log 文本,但收到 .fail 消息,内容为:
Request failed: parsererror
控制器的响应如下:
<?php{"id":29,"user_id":1,"body":"Sunday evening post","created_at":"2016-10-09 23:03:11","updated_at":"2016-10-09 23:03:11","user":{"id":1,"firstname":null,"lastname":null,"username":"pathros","email":"pathros@somemail.net","created_at":"2016-10-08 05:33:06","updated_at":"2016-10-08 18:57:19"}}
我注意到响应添加了 <?php
。可能是 Chrome 的检查元素工具?
我错过了什么?
(注意:我使用的是 jQuery 3.1.1 和 Laravel 5.3)
您的问题与 AJAX 上的 dataType
选项有关。您当前有 dataType: 'json',
这意味着当服务器 returns a JSON.
时请求成功
如果您要返回类似于 return 'ok';
的内容,那么 AJAX 将会失败。
确保您返回的内容类似于:
return Response::json(['ok' => 'true']);
我有一个简单的表单,用户应该只在文本区域内键入文本。当用户单击 javascript 内的提交按钮时,我使用 vue 和 ajax 进行调用以将该文本插入数据库。
我的问题是,在用户点击提交后,我希望清除文本区域,但是在将文本保存到数据库后,文本仍然存在。它持续存在。由于我正在使用 vue.js 和 ajax,因此我正在等待这个 .done(成功)回调函数以继续清除表单。
或者如果文本已保存到数据库中,是否还有其他清除文本区域的想法?
这是我的 blade 形式的代码:
<div class="row" id="timeline">
<div class="col-md-4">
<form action="#" v-on:submit="postStatus">{{-- Name of the method in Vue.js --}}
<div class="form-group">
<textarea class="form-control" rows="5" maxlength="140" autofocus placeholder="What are you upto?" required v-model="post" id="textareapost"></textarea>
</div>
<input type="submit" value="Post" class="form-control btn btn-info">
{{ csrf_field() }}
</form>
</div>
<div class="col-md-8">
Timeline
</div>
</div>
这是我的控制器:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use App\Http\Requests;
class PostController extends Controller
{
public function create(Request $request, Post $post)
{
//$request->body;
//Remember, all we really need is the body. Because if create this post through
//the User model, it'll automatically assign us a user_id
//validation:
$this->validate($request,[
'body' => 'required|max:140',
]);
//die('Posted in the controller!: '.$request->body); //$request->get('body');
//creating the post from the user, from the post that belongs to the user:
$createdPost = $request->user()->posts()->create([
'body' => $request->body,
]);
//Now respond with the post that has been created:
return response()->json($post->with('user')->find($createdPost->id));
}
}
到目前为止 javascript 代码:
var csrf_token = $('meta[name="csrf-token"]').attr('content');
/*Event handling within vue*/
//when we actually submit the form, we want to catch the action
new Vue({
el : '#timeline',
data : {
post : '',
token : csrf_token,
},
methods : {
postStatus : function (e) {
e.preventDefault();
console.log('Posted: '+this.post+ '. Token: '+this.token);
var request = $.ajax({
url : '/posts',
method : "POST",
dataType : 'json',
data : {
'body' : this.post,
'_token': this.token,
}
}).done(function (msg) {
console.log('Data saved: '+msg);
this.post = '';/*Attempting to clear the form*/
}.bind(this));/*
request.done(function( msg ) {
console.log('Data saved: '+msg+'. Outside ...');
//$( "#log" ).html( msg );
});
request.fail(function( jqXHR, textStatus ) {
console.log( "Request failed: " + textStatus );
});
}
},
});
保存文本后,我从未收到 .done 部分的 console.log 文本,但收到 .fail 消息,内容为:
Request failed: parsererror
控制器的响应如下:
<?php{"id":29,"user_id":1,"body":"Sunday evening post","created_at":"2016-10-09 23:03:11","updated_at":"2016-10-09 23:03:11","user":{"id":1,"firstname":null,"lastname":null,"username":"pathros","email":"pathros@somemail.net","created_at":"2016-10-08 05:33:06","updated_at":"2016-10-08 18:57:19"}}
我注意到响应添加了 <?php
。可能是 Chrome 的检查元素工具?
我错过了什么?
(注意:我使用的是 jQuery 3.1.1 和 Laravel 5.3)
您的问题与 AJAX 上的 dataType
选项有关。您当前有 dataType: 'json',
这意味着当服务器 returns a JSON.
如果您要返回类似于 return 'ok';
的内容,那么 AJAX 将会失败。
确保您返回的内容类似于:
return Response::json(['ok' => 'true']);