jQuery的AJAX处理问题 [英] jQuery AJAX HAndling problem
问题描述
我试图创建将处理所有我AJAX的调用,而不是做几个不同的AJAX的处理程序有一个做这项工作的所有的JavaScript对象。
I am trying to create a JavaScript object that will deal with all of my ajax calls instead of making several different ajax handlers have one that does the job for all.
所以,这里是我迄今为止
So here's what I have so far
我的PHP文件都放在一个目录中称为AJAX,在被命名涉及到的网页,例如目录招聘信息网页有自己的目录等。这些目录内的我已经把网页相关的PHP文件。
My PHP files that are going to be called within my AJAX Handler are placed in a directory called ajax, the directories within that are named to relate to the pages e.g. the jobs page has its own directory and so on. within these directories I have placed the page relevant PHP files.
所以现在我的AJAX Handler对象code:
so now to my ajax Handler object code:
function ajaxHandler(pageName,functionCall){
this.pageName = pageName;
this.functionCall = functionCall;
// set functions
this.getPage = getPage;
this.setPage = setPage;
this.getFunctionCall = getFunctionCall;
this.setFunctionCall = setFunctionCall;
this.performAjaxCall = performAjaxCall;
}
// accessor for current page
function getPage(){
return this.pageName;
}
// accessor for setting the current page
function setPage(page){
this.pageName = page;
}
// accessor for retrieving the current functionCall
function getFunctionCall(){
return this.functionCall;
}
// accessor for setting the current function call
function setFunctionCall(func){
this.functionCall = func;
}
// perform the loaded ajax call
// DATA : must be in the form of JSON
function performAjaxCall(data){
$.ajax({
type : 'POST',
url : '/ajax/'+ this.pageName + '/' + this.functionCall + '.php',
dataType : 'json',
data : data,
success : function(data){
return data;
},
error : function(xhr, ajaxOptions, thrownError){
return {"error":true,"msg":thrownError};
}
})
}
所以用这个文件调用,从什么时候performAjaxCall被称为AJAX做工精细,除了完美的作品,但返回的数据被视为不确定我会告诉你一个例子
so with this file called and working fine, apart from when performAjaxCall is called the ajax works perfectly but the data being returned is being seen as undefined I will show you an example
function getActiveJobs(initPage){
var ajh = new ajaxHandler('jobs','getActiveJobs');
var req = {'page' : 1};
var data = ajh.performAjaxCall(req);
alert(data.error);
}
警报返回undefined,我怀疑这是因为JavaScript是不是在等待要返回的数据,因此数据尚未确定,但我不确定我想我会问这里才导致自己下了死胡同
The alert returns undefined, I suspect it is because the JavaScript is not waiting for the data to be returned and as such data is not yet defined, but I am unsure I thought I would ask here before leading myself down a blind alley.
推荐答案
您是对的,JavaScript是不是在等待。 performAjaxCall 请求的响应之前返回<$ C C $>被接收。但是,即使是在等待,你不能从一个回调像这样的返回值:
You are right, JavaScript is not waiting. performAjaxCall
returns before the response of request is received. But even it were waiting, you cannot return a value from a callback like this:
success : function(data){
return data;
},
正在通过这个功能来 $。阿贾克斯
,所以它返回东北值仅在 $。阿贾克斯
功能。它不会对外部 performAjaxCall
功能产生任何影响。
You are passing this function to $.ajax
, so it returns ne value only inside the $.ajax
function. It does not have any effect on the outer performAjaxCall
function.
您必须通过另一个函数将处理数据,是这样的:
You have to pass another function which will process the data, something like:
function performAjaxCall(data, cb){
$.ajax({
//...
success: cb,
error: cb
});
}
和
function getActiveJobs(initPage){
var ajh = new ajaxHandler('jobs','getActiveJobs');
var req = {'page' : 1};
ajh.performAjaxCall(req, function(data) {
alert(data.error);
});
}
这篇关于jQuery的AJAX处理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!