Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9143531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:02:46+00:00 2026-06-17T10:02:46+00:00

Say I have this: for( i = 0; i < 10; i++ ) {

  • 0

Say I have this:

for( i = 0; i < 10; i++ )
{
    $.ajax({
        type: "POST",
        url: "geocode-items.php",
        async: true,
        data: {  
            key: i
        },
        success: function( data )
        {
            // data returns the index I passed into the script with the data property.
        },
        error( a, b, c )
        {
            alert( i ); // this obviously won't work
        }
    });    
}

alert( i ); in the error section is not going to alert the correct index. Whereas in the success I can be passing back the key I put in to the geocode-items.php script, I can’t pass anything back in the error section.

Do you know how to reference the original data sent through the request when firing the error method?

something like this.data.key? so I can report back an error for the specific object I’m stuck on? Rather than having to write some generic “there was an error code but I don’t know where”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:02:47+00:00Added an answer on June 17, 2026 at 10:02 am

    you should read something about the scopes of javascript and about closures.
    the value iis the same for each error callback in your case and because ajax is async the i is 10 for all of them.

    javascript only has functions based scopes and no block based scopes.
    What you can do is create an anonymous function where you pass your value to (function(param1) { } )(value) this function is called immediately. the parameters of the functions are then bound to that function call.

    for( i = 0; i < 10; i++ )
    {
        (function(idx) {
           $.ajax({
               type: "POST",
               url: "geocode-items.php",
               async: true,
               data: {  
                   key: idx
               },
               success: function( data )
               {
                   // data returns the index I passed into the script with the data property.
               },
               error: function( a, b, c )
               {
                   alert( idx ); // this obviously won't work
               }
            });
        })(i);   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm sending this simple ajax request (jquery): $.ajax({ type: 'POST', url: '/ajax/requestForm.php',
Imagine I run this: $.ajax({ type: 'POST', url: '/ajax/watch.php', data: {'watch':'aukcia', 'id':aukciaID}, complete: function(responseText){
Let's say I have the following jQuery AJAX call: $.ajax({ type: POST, url: MyUrl,
I know I can do this: //Jquery $.ajax({type: 'POST', data : {'action' : 'foo'}});
Let's say I have an Ajax call from jQuery like this: $.ajax({ url: myUrl,
Let's say I have this form : <form action=Change-status.php method=post> <select class=changeStatus name=changeStatus> <option
Lets say have this immutable record type: public class Record { public Record(int x,
Say I have this table schema. ID AccNo Amount Say I have this data
Say I have this url: http://site.example/dir/ In this folder I have these files: test.ascx.cs
here I have jQuery ajax calling a .php file that SOMETIMES executes let's say

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.