Click here to Skip to main content
15,899,023 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Dear Sir ,


Please give me solution :

My problem is :

C#
for (var i =0; i<5;i++){

    var a =i;
    //like this some java script code here ;
    $.ajax({
    
    url:"",
    type:"",
    dataType:"",
    success:function(){ //code code is fired in last and only last time.},
    error:function(){}
    });

}
Posted
Updated 22-Oct-15 9:59am
v2
Comments
xszaboj 22-Oct-15 15:55pm    
sorry what is the problem again? You want to call //code code... just when i==4? or you want to make ajax call just once?
ZurdoDev 22-Oct-15 16:11pm    
It's not clear what you are asking.
Sergey Alexandrovich Kryukov 22-Oct-15 17:47pm    
This is not a question at all. Anyway, the bug looks very obvious; please see Solution 1.
—SA
ZurdoDev 22-Oct-15 20:03pm    
Not exactly. OP did not post the code, just the skeleton code.
Sergey Alexandrovich Kryukov 22-Oct-15 20:17pm    
No matter, this fragment won't work in any possible case.
—SA

1 solution

It's not clear what are you asking about; and this is not a question, but — it's apparent that your code is wrong: under the success function implementation, you try to use something similar to object property definition syntax "error:function(){}", which is invalid under the function body block "{}", could only be valid under the object-definition block. It throws the following exception:
SyntaxError:
function statement requires a name
(Exception detail may depend on the browser.)

As this is a syntax error, you could not even catch it as if you normally could do with other exceptions; it makes your whole script defunct. I have no idea how could you observe "code code is fired in last and only last time" and what it could possibly mean. For further detail on dealing with lexical errors, please see the section in my article: JavaScript Calculator, 5. Handling Lexical Errors.

However, you should better just fix the syntax. Just look more thoroughly at jQuery Ajax usage:
http://api.jquery.com/jquery.ajax[^],
http://api.jquery.com/category/ajax[^].

—SA
 
Share this answer
 
v2
Comments
ZurdoDev 22-Oct-15 20:03pm    
OP did not post all the code.
Sergey Alexandrovich Kryukov 22-Oct-15 20:17pm    
I commented on that — it doesn't matter.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900