使用挂钩/延迟与 xhrGet 请求
Using hitch / deferred with an xhrGet request
我试图使用deferred
和hitch
来为我的AJAX请求提供回调。我正在使用以下代码:
//Code is executed from this function
function getContent(){
var def = new dojo.Deferred();
def.then(function(res){
console.lod("DONE");
console.log(res);
},
function(err){
console.log("There was a problem...");
});
this.GET("path", def);
}
//Then GET is called to perform the AJAX request...
function GET(path, def){
dojo.xhrGet({
url : path,
load : dojo.hitch(def,function(res){
this.resolve(res);
}),
error : dojo.hitch(def, function(err){
this.reject(err)
})
})
}
但是,当我运行此代码时,我在this.resolve(res)
上收到undefined method
错误。我已经打印了this
(解析为延迟对象)和res
,两者都不是未定义的。为什么我会收到此错误以及如何实现我尝试执行的操作?
ajax 方法(xhrGet 和 xhrPost)在执行时返回延迟对象。使用此延迟对象,您可以在 ajax 请求完成时注册回调和错误回调处理程序
var deferred = dojo.xhrGet({url:'someUrl'});
deferred.then(dojo.hitch(this,this.someCallbackFunction),dojo.hitch(this,this.someErrorFunction));
使用此代码,当您的 ajax 请求成功返回时将调用someCallbackFunction
,如果 ajax 请求返回错误,则将调用someErrorFunction
。
对于您的特定代码段,您的代码更新如下:
function getContent(){
var resultFunction = function(res){
console.lod("DONE");
console.log(res);
};
var errorFunction = function(err){
console.log("There was a problem...");
};
this.GET("path").then(dojo.hitch(this,resultFunction),dojo.hitch(this,errorFunction);
}
//Then GET is called to perform the AJAX request...
function GET(path){
return dojo.xhrGet({
url : path
});
}
相关文章:
- ajax请求的顺序总是不同的
- Meteor如何接收HTTP请求
- 有没有一种方法可以防止img get请求使用css或js发生
- 从ajax请求中获取javascript对象
- JSONP请求返回结果,但也触发error_callback
- 在localhost Dev Box上测试JSONP请求的最佳方式
- Ajax请求文档就绪会导致jquery加载缓慢
- MockJax没有在JavaScript应用程序中发送对我AJAX请求的响应
- 正在传递JSONP标头's数据参数到另一个文件中的AJAX请求
- 在openshift node js应用程序中获取请求
- 反应路由器弄乱了请求网址
- 在我的情况下,如何进行http请求
- 使用密码对话框Javascript请求帮助
- servlet中的请求对象,而不是从jsp接收参数值
- 否'访问控制允许来源'标头存在于IISNOde中请求的资源(AngularJS+NodeJs)上
- JavaScript代码未正确检查ajax请求
- node.js请求数据事件未在CORS ajax调用中触发
- 如何检测第三方广告服务器请求
- 在Rails中更新Div,而不更改更新请求后的视图
- 使用挂钩/延迟与 xhrGet 请求