Javascript 如何在vue.js 2上获得响应ajax?
我的代码如下:Javascript 如何在vue.js 2上获得响应ajax?,javascript,ajax,vue.js,laravel-5.3,vuejs2,Javascript,Ajax,Vue.js,Laravel 5.3,Vuejs2,我的代码如下: <template> <a href="javascript:" class="btn btn-block btn-success" @click="addFavoriteStore($event)"> <span class="fa fa-heart"></span> Favorite </a> </template> <script>
<template>
<a href="javascript:" class="btn btn-block btn-success" @click="addFavoriteStore($event)">
<span class="fa fa-heart"></span> Favorite
</a>
</template>
<script>
export default{
props:['idStore'],
mounted(){
this.checkFavoriteStore()
},
methods:{
addFavoriteStore(event){
alert('tost');
event.target.disabled = true
const payload= {id_store: this.idStore}
this.$store.dispatch('addFavoriteStore', payload)
setTimeout(function () {
location.reload(true)
}, 1500);
},
checkFavoriteStore(){
const payload= {id_store: this.idStore}
this.$store.dispatch('checkFavoriteStore', payload)
setTimeout(function () {
location.reload(true)
}, 1500);
//get response here
}
}
}
</script>
import { set } from 'vue'
import favorite from '../../api/favorite'
import * as types from '../mutation-types'
// actions
const actions = {
checkFavoriteStore ({ dispatch,commit,state },payload)
{
favorite.checkFavorite(payload,
data => {
commit(types.CHECK_FAVORITE_SUCCESS)
},
errors => {
commit(types.CHECK_FAVORITE_FAILURE)
console.log(errors)
}
)
}
}
// mutations
const mutations = {
[types.CHECK_FAVORITE_SUCCESS] (state){
state.addStatus = 'success'
},
[types.CHECK_FAVORITE_FAILURE] (state){
state.addStatus = 'failure'
}
}
export default {
actions,
mutations
}
import Vue from 'vue'
import Resource from 'vue-resource'
Vue.use(Resource)
export default {
// api check favorite exist or not
checkFavorite (favorite, cb, ecb = null ) {
Vue.http.post(window.Laravel.baseUrl+'/member/store/favorite/check-favorite', favorite)
.then(
(resp) => cb(resp.data),
(resp) => ecb(resp.data)
);
}
}
api是这样的:
<template>
<a href="javascript:" class="btn btn-block btn-success" @click="addFavoriteStore($event)">
<span class="fa fa-heart"></span> Favorite
</a>
</template>
<script>
export default{
props:['idStore'],
mounted(){
this.checkFavoriteStore()
},
methods:{
addFavoriteStore(event){
alert('tost');
event.target.disabled = true
const payload= {id_store: this.idStore}
this.$store.dispatch('addFavoriteStore', payload)
setTimeout(function () {
location.reload(true)
}, 1500);
},
checkFavoriteStore(){
const payload= {id_store: this.idStore}
this.$store.dispatch('checkFavoriteStore', payload)
setTimeout(function () {
location.reload(true)
}, 1500);
//get response here
}
}
}
</script>
import { set } from 'vue'
import favorite from '../../api/favorite'
import * as types from '../mutation-types'
// actions
const actions = {
checkFavoriteStore ({ dispatch,commit,state },payload)
{
favorite.checkFavorite(payload,
data => {
commit(types.CHECK_FAVORITE_SUCCESS)
},
errors => {
commit(types.CHECK_FAVORITE_FAILURE)
console.log(errors)
}
)
}
}
// mutations
const mutations = {
[types.CHECK_FAVORITE_SUCCESS] (state){
state.addStatus = 'success'
},
[types.CHECK_FAVORITE_FAILURE] (state){
state.addStatus = 'failure'
}
}
export default {
actions,
mutations
}
import Vue from 'vue'
import Resource from 'vue-resource'
Vue.use(Resource)
export default {
// api check favorite exist or not
checkFavorite (favorite, cb, ecb = null ) {
Vue.http.post(window.Laravel.baseUrl+'/member/store/favorite/check-favorite', favorite)
.then(
(resp) => cb(resp.data),
(resp) => ecb(resp.data)
);
}
}
如果您返回一个结果,则将调用分配给一个变量,以便tyr:
var res = this.$store.dispatch('checkFavoriteStore', payload);
然后,您可以通过varres
访问响应。我相信这些事件会把它还给你
更多查询:
我只是想问一个小问题
如果您正在获取存储ID并将其作为道具传递给组件。那么,你当然也可以通过另一个道具来判断这家商店是否已经受到欢迎了?因此,通过对视图的db调用获取这些数据,从而节省了在加载数据后进行检查的时间,例如:
<favorite-button :storeid="23" :favorited="true"></favorite-button>
我添加了console.logs,这样我们就可以看到发生了什么。让我们看看这是否有帮助
还可以尝试在前面添加一个return,因为它需要返回到其原始调用者
return favorite.checkFavorite(payload,
data => {
commit(types.CHECK_FAVORITE_SUCCESS)
},
errors => {
commit(types.CHECK_FAVORITE_FAILURE)
console.log(errors)
}
)
还有一个注释,您是否需要所有这些复杂度来进行简单的检查,我不知道您的代码和构造的其余部分,但正如建议的那样,将按钮的状态作为道具传递(如上所述),只需在组件本身内处理isFavorited调用,因此无需使用存储来处理这个简单的请求
编辑2:
如果您需要返回另一个带承诺的wahy,请尝试:
Vue.$http.post(window.Laravel.baseUrl+'/member/store/favorite/check-favorite', favorite)
.then((response) => {
console.log('success')
resolve(response.data);// replace the return with resolve?
}, (response) => {
console.log('success');
console.log(response);
reject(response.data);// replace the return with reject?
});
可能是问题所在???如果您返回一个结果,则将调用分配给一个变量,以便tyr:
var res = this.$store.dispatch('checkFavoriteStore', payload);
然后,您可以通过varres
访问响应。我相信这些事件会把它还给你
更多查询:
我只是想问一个小问题
如果您正在获取存储ID并将其作为道具传递给组件。那么,你当然也可以通过另一个道具来判断这家商店是否已经受到欢迎了?因此,通过对视图的db调用获取这些数据,从而节省了在加载数据后进行检查的时间,例如:
<favorite-button :storeid="23" :favorited="true"></favorite-button>
我添加了console.logs,这样我们就可以看到发生了什么。让我们看看这是否有帮助
还可以尝试在前面添加一个return,因为它需要返回到其原始调用者
return favorite.checkFavorite(payload,
data => {
commit(types.CHECK_FAVORITE_SUCCESS)
},
errors => {
commit(types.CHECK_FAVORITE_FAILURE)
console.log(errors)
}
)
还有一个注释,您是否需要所有这些复杂度来进行简单的检查,我不知道您的代码和构造的其余部分,但正如建议的那样,将按钮的状态作为道具传递(如上所述),只需在组件本身内处理isFavorited调用,因此无需使用存储来处理这个简单的请求
编辑2:
如果您需要返回另一个带承诺的wahy,请尝试:
Vue.$http.post(window.Laravel.baseUrl+'/member/store/favorite/check-favorite', favorite)
.then((response) => {
console.log('success')
resolve(response.data);// replace the return with resolve?
}, (response) => {
console.log('success');
console.log(response);
reject(response.data);// replace the return with reject?
});
可能是问题所在???下面是一个不需要商店的解决方案,只是一个想法可能很方便: PHP页面:
<favorite-button
:storeid="{{$storeid}}"
:favorited="{{$isFavorited}}"
:url="{{route('check-favorite')}}"
></favorite-button>
<script>
(function(){
import favoriteButton from 'favorite-button';
new Vue({
el : '#app',
components : {
favoriteButton
}
});
})();
</script>
(功能(){
从“收藏夹按钮”导入收藏夹按钮;
新Vue({
el:“#应用程序”,
组成部分:{
收藏夹按钮
}
});
})();
然后是组件
<style lang="sass">
.heart {
color: grey;
&.is-favorited {
color: red;
}
}
</style>
<template>
<button class="btn btn-block btn-success" @click.prevent="udateFavorited($event)">
<span class="heart" :class="{'is-favorited' : this.favorited }">&heart;</span> <span :text="{this.favoriteText}"></span>
</button>
</template>
<script>
import axios from 'axios';
export default{
props : {
storeid : {
type: Number,
required : true,
default : () => {}
},
favorited : {
type: Boolean,
required : false,
default : false
},
url : {
type: String,
required : true
}
},
computed : {
favoriteText : function(){
return (this.favorited) ? 'Unfavorite' : 'Favorite';
}
},
methods:{
updateFavorited(event){
//-- so update the db so if fasle update to true else if true = false
axios.post(this.url, this.storeid)
.then(response => {
//-- all ok update the visual button
this.favorited = response.data.favorited
})
.catch(error => {
console.log('error');
console.log(error);
this.favorited = response.data.favorited
});
}
}
}
</script>
.心脏{
颜色:灰色;
&.受欢迎{
颜色:红色;
}
}
&心;
从“axios”导入axios;
导出默认值{
道具:{
storeid:{
类型:数字,
要求:正确,
默认值:()=>{}
},
偏爱:{
类型:布尔型,
必填项:false,
默认值:false
},
网址:{
类型:字符串,
必填项:true
}
},
计算:{
favoriteText:函数(){
return(this.favorited)-‘Favorite’:‘Favorite’;
}
},
方法:{
updateFavorited(事件){
//--因此,如果fasle更新为true,则更新数据库,否则如果true=false
post(this.url,this.storeid)
。然后(响应=>{
//--所有ok更新可视按钮
this.favorited=response.data.favorited
})
.catch(错误=>{
console.log('error');
console.log(错误);
this.favorited=response.data.favorited
});
}
}
}
因此,基本上在初始加载页面时,它会传递storeid,以及该存储是否已被偏爱,以及单击事件时的url操作
然后,当用户单击按钮时,它将根据结果更新数据库,然后更新文本和心脏颜色
只是另一个想法/解决方案来考虑是否有问题??
下面是一个不需要存储的解决方案,只是一个想法可能是方便的:
PHP页面: <favorite-button
:storeid="{{$storeid}}"
:favorited="{{$isFavorited}}"
:url="{{route('check-favorite')}}"
></favorite-button>
<script>
(function(){
import favoriteButton from 'favorite-button';
new Vue({
el : '#app',
components : {
favoriteButton
}
});
})();
</script>
(功能(){
从“收藏夹按钮”导入收藏夹按钮;
新Vue({
el:“#应用程序”,
组成部分:{
收藏夹按钮
}
});
})();
然后是组件
<style lang="sass">
.heart {
color: grey;
&.is-favorited {
color: red;
}
}
</style>
<template>
<button class="btn btn-block btn-success" @click.prevent="udateFavorited($event)">
<span class="heart" :class="{'is-favorited' : this.favorited }">&heart;</span> <span :text="{this.favoriteText}"></span>
</button>
</template>
<script>
import axios from 'axios';
export default{
props : {
storeid : {
type: Number,
required : true,
default : () => {}
},
favorited : {
type: Boolean,
required : false,
default : false
},
url : {
type: String,
required : true
}
},
computed : {
favoriteText : function(){
return (this.favorited) ? 'Unfavorite' : 'Favorite';
}
},
methods:{
updateFavorited(event){
//-- so update the db so if fasle update to true else if true = false
axios.post(this.url, this.storeid)
.then(response => {
//-- all ok update the visual button
this.favorited = response.data.favorited
})
.catch(error => {
console.log('error');
console.log(error);
this.favorited = response.data.favorited
});
}
}
}
</script>
.心脏{
颜色:灰色;
&.受欢迎{
颜色:红色;
}
}
&心;
从“axios”导入axios;
导出默认值{
道具:{
storeid:{
类型:数字,
要求:正确,
默认值:()=>{}
},
偏爱:{
类型:布尔型,
必填项:false,
默认值:false
},
网址:{
类型:字符串,
必填项:true
}
},
计算:{
favoriteText:函数(){