Javascript 未捕获类型错误:无法设置属性';答复';未定义的
所以,我有我在2015年写的旧代码,它当时运行良好,但现在它不——有什么问题吗?在使用ajax时,它似乎位于请求函数中,我得到一个错误:Javascript 未捕获类型错误:无法设置属性';答复';未定义的,javascript,jquery,http,xmlhttprequest,Javascript,Jquery,Http,Xmlhttprequest,所以,我有我在2015年写的旧代码,它当时运行良好,但现在它不——有什么问题吗?在使用ajax时,它似乎位于请求函数中,我得到一个错误: VM3641:326 Uncaught TypeError: Cannot set property 'response' of undefined at Object.success (<anonymous>:326:48) at i (jquery.min.js:2) at Object.fireWith [as resolveWith] (j
VM3641:326 Uncaught TypeError: Cannot set property 'response' of undefined
at Object.success (<anonymous>:326:48)
at i (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at y (jquery.min.js:4)
at XMLHttpRequest.c (jquery.min.js:4)
这是因为
this
内部success
和error
回调与它外部的this
不同。所以ResponseData
恰好是未定义的
,因此它不能有response
属性。尝试使用:
将使回调的作用域设置为您以前的
this
,这是因为this
内部成功
和错误
回调与外部的this
不同。所以ResponseData
恰好是未定义的
,因此它不能有response
属性。尝试使用:
将使回调的作用域设置为那些函数中以前的
thisthis
(成功和错误
)的作用域不会是类的实例,因此this.ResponseData
在那些函数中是未定义的this
(success
和error
)将不会是您的类的实例,因此这个。ResponseData
是未定义的。或者只需将这个存储在一个变量中,该变量在外部并在内部使用。@ibrahimmahrir它也可以工作,但我不建议使用它,因为我发现它不如使用bind()优雅)
,imo。或者只需将这个存储在一个变量中,该变量在外部
并在内部使用。@ibrahimmahrir它也可以工作,但我不建议这样做,因为我发现使用bind()
,imo没有这么优雅。
success: function (response) {
console.log(response);
this.ResponseData.response = response;
}.bind(this),
error: function (err, errCode, errMessage) {
this.ResponseData.response = '';
this.ResponseData.hasErrors = true;
console.log('error');
}.bind(this)