函数内的 ajax 调用

发布于 2024-09-19 17:33:19 字数 595 浏览 10 评论 0原文

我有一个执行某些任务的函数。在函数内部我有一个ajax 调用。如果从 ajaxcall 获得的响应为 true,则该函数应继续执行任务的其余部分。否则,它应在该点处停止。 但是上面所说的事情没有发生,而是函数独立于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
}

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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

套路撩心 2024-09-26 17:33:19

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

The function "f1" has a formal parameter called "id"; that's the variable it will test in the statement "if(id==true)". While it may be true that there is also a global variable called "id", and that the callback function will access and modify it, it is a different variable. A modification to it will not affect the test in f1.

掌心的温暖 2024-09-26 17:33:19

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

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

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

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文