函数内的 ajax 调用
我有一个执行某些任务的函数。在函数内部我有一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
函数“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.
请参阅我对此相关问题的回答:如何在发布或循环时在 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: