在$中获得成功或完成响应的问题.当在Laravel中使用vue.js时
Problems getting success or done response in $.ajax when using vue.js in Laravel
我有这个简单的表单,用户应该只在文本区域内输入文本。当用户单击提交按钮时,在javascript中,我调用vue和ajax将该文本插入数据库。
我的问题是,在用户点击提交后,我希望文本区域被清除,但在文本保存到数据库后,文本仍然存在。它仍然存在。由于我正在使用vue.js和ajax,因此我正在等待。done(成功)回调函数,以便继续清除表单。
或者如果文本保存到数据库中,是否有其他方法来清除文本区域?
我的刀片代码如下:
<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));/*http://stackoverflow.com/a/26479602/1883256*/
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消息,上面写着:
请求失败: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',
,这意味着当服务器返回JSON时请求成功。
如果您返回类似return 'ok';
的东西,AJAX将失败。
确保你返回的内容是:
return Response::json(['ok' => 'true']);
相关文章:
- 当在Ember中点击一个项目时,我如何将一个活动类添加到项目列表中
- 当#在iFrame中使用HTML时,阻止页面移动
- 当在文本框中搜索关键字时,我会得到以前的结果作为输出
- 当在文档中拖动mouseup/mouseleve时,我想调用我的Ajax请求
- jqueryajax调用是复制url,当在模态视图之后在外部js文件中进行调用时
- 当在coldfusion中通过AJAX传递时,FORM提交在IE浏览器中给出空表单
- 当在输入框中聚焦时,清除页面上的所有间隔
- -当在JQuery Slider上添加-webkit线性梯度时,moz线性梯度会被破坏
- AngularJS$apply有时不会;当在highchart中使用click事件时,t触发更新{{}}中html标记中
- 当在ng视图中加载新路由时,Tabbing(tabindex)针对错误的元素
- 当在EXTJS中的网格中使用时,afterrender不适用于组合框
- 为什么$q.当在Angular中后跟$q.拒绝时.js会导致成功
- 当在一个表单中进行更改时,ng-disabled 会触发所有表单验证
- 当在文本框中按下Enter键时,重定向到page.aspx
- 在正则表达式中,如果第一个圆括号没有't匹配,当在JavaScript中使用replace()与在Ruby中使用
- 当在外部js中时,Highcharts条形图中断
- 当在document.ready中使用async=true时会发生什么
- 如何“;postMessage”;当在IE9对象元素中查看时,从pdf中托管
- 仅当在文本框中输入文本时显示X按钮
- 在$中获得成功或完成响应的问题.当在Laravel中使用vue.js时