ajax调用函数内部

时间:2010-09-15 18:40:28

标签: ajax ajax.beginform

我有一个功能,它执行一些任务。在我有一个ajax调用的函数中。如果从ajaxcall获得的响应为真,则该函数应该继续执行任务的其余部分。否则它应该在该点停止。 但是上面说的事情没有发生,而是函数独立于ajax调用执行。 请帮帮我

function f1(id)
{
var test= id
var url="contentserver?pagename=mandatory@id"=+id;
var ajax=new Ajaxrequest(url,validatecallback);
ajax.doGet();
if(id==true){
return;
}
........(some code which has to carried out after the ajax call)
}
function validatecallback
{
this function gets the response for the above mentioned ajaxcall
we are setting a global variable(i.e id) here so that we can use that we can retrieve that in function f1
}

2 个答案:

答案 0 :(得分:0)

函数“f1”有一个名为“id”的形式参数;这是它将在语句“if(id == true)”中测试的变量。虽然可能确实存在一个名为“id”的全局变量,并且回调函数将访问和修改它,但它是一个不同的变量。对它的修改不会影响f1中的测试。

答案 1 :(得分:0)

请参阅我对此相关问题的回答:How can I write an async method in JavaScript when posting or looping?

基本上你必须改变你的想法。它需要改为:

function f1(id) {
  var test= id
  var url="contentserver?pagename=mandatory@id"=+id;
  var ajax=new Ajaxrequest(url,function (r) {
      validatecallback(r)
      if(id==true){
        return;
      }
      ........(some code which has to carried out after the ajax call)
  });
  ajax.doGet();
}