1

I would like to create a function around an ajax call so that I can re-use it. But I'm having trouble populating the data: ({ … }) part of it based on conditions (via a switch).

var d = {};
function lookup (what,etat,compt,vile,zip) {
    switch(what) {
        case "cou_mun":
            d: ({
                lookup: "county",
                state: etat
            })
            break;
        case "city":
            d: ({
                lookup: "city",
                state: etat,
                county: compt
            })
            break;
        case "zipcode":
            d: ({
                lookup: "zipcode",
                state: etat,
                county: compt,
                city: vile
            })
            break;
        default:break;
    }
    $.ajax({
        url: "socioGeo_envnLookup.php",
        type: "POST",
        data: d,
            //lookup: "county", //"county" or "city" or "zipcode"
            //state: etat
            //county: cou_mun,
            //city: city,
        success: function(what,data) {
            $('select[name="'+what+'"]').html(data);
        }
    }); //$.ajax
} //lookup()

And then I would call it like lookup("city",state,county);

IE complains when I try to make an ajax call that sends undefined variables, so I want to set data: ({ … }) dynamically.

2
  • 2
    You have to assign a value to d which is done by the assignment operator =. Also make sure that you define d locally in your function. Commented Mar 2, 2012 at 19:07
  • @FelixKling Do you know why what in success: as the value that should be in data? I did alert('what = '+what+'\n'+'data = '+data.toSource); inside of success and what = the html that should be in data and data = function toSource() { [native code] } Commented Mar 2, 2012 at 20:18

1 Answer 1

4
d: ({
            lookup: "county",
            state: etat
        })

Should be

d = {
        lookup: "county",
        state: etat
    };
Sign up to request clarification or add additional context in comments.

5 Comments

ah, that does help. Do you know why what in success: as the value that should be in data? I did alert('what = '+what+'\n'+'data = '+data.toSource); inside of success and what = the html that should be in data and data = function toSource() { [native code] }
success has the following signature: success(data, textStatus, jqXHR). The first parameter is the data, the 2nd is the status. That is why what is the data. It doesn't matter what you name that parameter, jquery will pass the data as the first parameter.
Ah. Okay. How do I pass what to success so I can load the data into a specified select?
You don't have to pass it, just use it. It will be accessible inside of the success function.
Ohh, that's unexpected. Thanks! I'll try when I have server-access on Monday :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.