我正在获得ajax响应
{ pid: 6, status: true }
我需要存储pid
并在下一个ajax post请求中传递它。
我目前的vue js如下
submitBox = new Vue({
el: "#submitBox",
data: {
name: '',
gstno: '',
about: '',
pid: '',
},
methods: {
handelSubmit: function(e) {
var vm = this;
data = {};
data['name'] = this.name;
data['gstno'] = this.gstno;
data['about'] = this.about;
$.ajax({
url: 'alpha/add/post/',
data: data,
type: "POST",
dataType: 'json',
success: function(e) {
if (e.status) {
alert(" Success")
} else {
vm.response = e;
alert(" Failed")
}
}
});
return false;
}
},
});
我需要将pid
存储为全局变量,并对所有ajax请求使用相同的内容。我怎样才能做到这一点?
小东西,如果你有一个小应用程序,并没有太多的架构或js
驻留在该页面上只有你可以使用全局变量
使用
vuex
这个小东西是不可取的(如果他已经使用它会很好)
当你收到ajax响应时,你可以在全局设置id
if (e.status) {
window._postPid = e.pid; // assume `e` is response and e.pid is 6
}
else {
// according to your need.
// this is optional window._postPid = null;
}
现在,您可以随意访问它
console.log(window._postPid); // 6
对于大型应用程序,这不是首选。因为我们不想破坏全局命名空间或冲突变量。
但它会做你想要的。