开发者

I am receiving output if i "ALERT" some text else i didn't receive any o/p while using ajax with javascript and jquery

I am receiving a different type of error in my application.

In my project most of the functionality are in ajax. Now my prob is if i "ALERT" something i am receiving the output. Else i am not. I am totally confused with this issue and don't have any idea t开发者_如何转开发o solve it.

What will be the solution?

Any answers will be helpful and grateful..

thanks in advance


Thr A in AJAX is shorthand for "asynchronous", which means the call does not follow the ordinary time flow.

I mean, say you have this code

 alert(1);
 some_function();
 alert(2);

In javascript, you expect the system alert 1, then run some_function() and wait it to return something and only after it finishes, you see alert 2.

But if some_function() is ajax, you get alert 1, then alert 2, and you can get the result of some_function() in between those two alerts as well as after the second alert, because the ajax call works asynchrously, js wont wait for its response.

With that in mind, lets return to your problem.

You say, you dont get a result unless you alert something. If you alert something, javascript stops until the user clicks "ok", but ajax wont. So this will create a time delay, and during this time delay, ajax probably finishes running and returns result.

Your mistake is, i'm just guessing since you did dont provide any code, you run two ajax calls succesively and the latter one depends on the former one.

var x = some_ajax_function();
var y = some_other_function(x);

this wont work (probably), because second function is called before the value of x is assigned. But if you put an alert in between, you get the result, because the ajax call returns a result and assigns x in the time delay.

So, what you need is, you can redesign your logic, or you can call the second function in the success phase of the first ajax call.

Something like

function some_ajax_function()
{
    ....
    //ajax success state
    var x = ajax_result;
    some_other_function(x);
}

I hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜