即使阅读https://gist.github.com/domenic/3889970,我也可能无法完全理解Promise,我需要从axios中获得Sweetalert确认结果的响应。
这是我的代码
axios
.post("/posts", this.formData)
.then(response => {
if (typeof response.data.callback !== "undefined") {
toastr.info("Created");
swal.fire({
title: "Success!",
text: "you created new post",
type: "success",
showConfirmButton: true,
confirmButtonText: "Close this",
allowOutsideClick: false
}).then(result => {
if (result.value) {
window.location.replace(response.data.callback);
}
});;
} else {
toastr.error("Can't process this request");
}
this.formLoading = false;
})
响应是不确定的,我想我不明白范围在js中如何工作
答案 0 :(得分:0)
这里是您可以镜像的示例。它使用从成功的API调用接收到的URL(该URL在API响应中),然后发送另一个请求...这应该向您证明您可以在那儿做任何您想做的事...启动一个不同的功能...从字面上讲,*您想做的任何事,都可以在该.then(...)
块中做。.
CodeSandbox example (updated with chained alerts and API calls)
EDIT: 更新了我的答案,以显示如何使用从API调用中检索到的数据。...
// Modal.vue component - holds the SweetAlert2
<template>
<div>
<button @click="swal();">CLICK ME TO LOAD API DATA USING SWEETALERT2</button>
<div v-if="name != ''">
<p>Name: {{ name }}</p>
</div>
<div v-if="homeworld != ''">
<p>Homeworld: {{ homeworld }}</p>
</div>
</div>
</template>
<script>
export default {
data() {
return {
name: "",
homeworld: ""
};
},
methods: {
swal() {
let self = this;
this.$swal
.fire({
title: "Click OK to load from: https://swapi.co/api/people/1/",
showCancelButton: true,
confirmButtonText: "OK",
showLoaderOnConfirm: true,
preConfirm: () => {
return this.$axios
.get("https://swapi.co/api/people/1/")
.then(response => {
if (response.status != 200) {
throw new Error("Something went wrong");
}
return response.data;
})
.catch(error => {
this.$swal.showValidationMessage(`Request failed: ${error}`);
});
},
allowOutsideClick: () => !this.$swal.isLoading()
})
.then(result => {
if (result.value) {
let v = result.value;
self.name = v.name;
this.$swal
.fire({
title: "Click Ok to redirect to another API call",
text: `Going to (name:)"${v.name}"'s homeworld URL at: "${
v.homeworld
}"`,
showCancelButton: true,
confirmButtonText: "OK",
showLoaderOnConfirm: true,
preConfirm: () => {
return this.$axios
.get(v.homeworld)
.then(response => {
if (response.status != 200) {
throw new Error("Something went wrong");
}
return response.data;
})
.catch(error => {
this.$swal.showValidationMessage(
`Request failed: ${error}`
);
});
},
allowOutsideClick: () => !this.$swal.isLoading()
})
.then(res => {
if (res.value) {
self.homeworld = res.value.name;
this.$swal.fire({
title: "Homeworld",
text: JSON.stringify(res.value)
});
}
});
}
});
}
}
};
</script>
答案 1 :(得分:0)
我阅读了您的评论,您说回调是一个URL,之前我使用过axios和sweetalert,看来您需要以json格式传递“回调”。你完成了吗?如果不是,这是使用Laravel的示例。对不起,我的英语。
Controller.php
...
if(isAjax){
return response()->json(['status'=>'info','messages'=>'Winter is coming!','rheader'=>'Warning!']);
}
...
查看(使用Sweetalert的Vue),一般的sweetalert也应该工作
...
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
axios.post("{{route('someroute')}}",{id:xid}) //you can ignore this, this is just passing id to the route
.then(response => {
this.$swal(response.data.rheader, ""+response.data.messages+"", response.data.status);
if(response.data.status=="success"){
$("#something")).hide();
}
})
.catch(e => {
this.$swal("Warning", ""+e.response.data.messages+"", "warning");
});
...
希望它将对某人有所帮助:)