函数内部的ajax调用 [英] ajax call inside a function

查看:101
本文介绍了函数内部的ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个执行某些任务的函数。在该函数内部,我有一个ajax调用。如果从ajaxcall获得的响应为true,则该函数应继续执行其余任务,否则应在该点处停止。
但是上面所说的事情并没有发生,而是独立于ajax调用执行了该函数。
请帮助我解决这个问题

i have a function which does some task.inside the function i have an ajax call. if the response obtained from ajaxcall is true the function should continue with the rest of the task.else it should stop at that point itself. But the above said thing is not happening instead the function is executed independent of ajax call. please help me out in this

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
}


推荐答案

请参阅我对以下相关问题的回答:在发布或发布时如何在JavaScript中编写异步方法

See my answer to this related question: How can I write an async method in JavaScript when posting or looping?

基本上,您必须改变思维方式。相反,它应该是这样的:

Basically you have to change your thinking. It needs to instead be something like:

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();
}

这篇关于函数内部的ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆