Javascript 将surveyjs结果发送到API
我正在尝试将surveyjs结果发送到我的API 在mounted()中,我使用Javascript 将surveyjs结果发送到API,javascript,vue.js,vue-resource,surveyjs,Javascript,Vue.js,Vue Resource,Surveyjs,我正在尝试将surveyjs结果发送到我的API 在mounted()中,我使用vue资源发出GET请求,从数据库中获取问题,然后设置surveyjs。为了发送结果,我尝试在surveyJSonComplete函数中使用this.$http.post,但是我得到了无法读取未定义的属性“post”。另外,我试图在结果变量上加一个手表,但它不起作用 mounted() { this.$http .get("myAPI") .then(res => res.jso
vue资源发出GET
请求,从数据库中获取问题,然后设置surveyjs。为了发送结果,我尝试在surveyJSonComplete
函数中使用this.$http.post
,但是我得到了无法读取未定义的属性“post”。另外,我试图在结果变量上加一个手表,但它不起作用
mounted() {
this.$http
.get("myAPI")
.then(res => res.json())
.then(questions => {
this.questions = questions;
this.survey = new SurveyVue.Model(this.questions.pesquisa);
this.survey.locale = "pt";
this.survey.onComplete.add(function(survey) {
this.result = survey.data;
this.$http
.post(
`myAPI`,
this.result,
{ headers: { "Content-Type": "application/json" } }
)
.then(response => {
console.log(response);
UIkit.notification({
message: "Success",
pos: "top-center",
status: "success"
});
})
.catch(error => {
console.log(error);
UIkit.notification({
message: "Erro",
pos: "top-center",
status: "danger"
});
});
});
})
.catch(error => {
console.log(error);
UIkit.notification({
message: "Error",
pos: "top-center",
status: "danger"
});
});
}
要访问onComplete.add()
参数中的此
,可以用箭头函数替换常规函数:
this.survey.onComplete.add(survey=>{
this.result=survey.data;
/*剩下的代码*/
})
另一种方法是将此
放入变量中,该变量可用于访问外部此
:
const=this;
this.survey.onComplete.add(函数(调查){
结果=调查数据;
/*剩下的代码*/
})
阅读更多关于
其要点是,在函数内部,函数的this
覆盖组件的this
,除非它是一个箭头函数,故意没有this
,因此外部函数可用