25

I'm trying to use jQuery to call some custom API via Ajax/$.getJSON.

I'm trying to pass a custom value into the Ajax callback method, but that value is not getting passed through and is actually getting overwritten. This is my code:

var locationType = 3;
var url = 'blah blah blah' + '&locationType=' + locationType;

$("#loading_status").show();

$.getJSON(url, null, function(results, locationType) {
    searchResults(results, locationType)
});

The value of locationType BEFORE I call the URL using AJAX is 3. But after the call returns the data successfully, the value for locationType is now success. This is because the method signature of the callback is:

callback(data, textStatus)A callback function that is executed if the request succeeds.

How can I pass 1 or more parameters to a callback method?

2
  • locationType variable is global variable so you don't need to put it as parameter, in anonimus callback function that variable is free so it is search in surrounding environment which in this case is global environment. Commented Aug 7, 2010 at 12:23
  • 1
    @jcubic - It's not a global variable (well it might be, but probably not), more accurately it's available in the scope he's concerned about. Commented Aug 7, 2010 at 12:26

6 Answers 6

20

You don't need to pass it in, just reference the variable you already have, like this:

var locationType = 3;
var url = 'blah blah blah' + '&locationType=' + locationType;
$("#loading_status").show();
$.getJSON(url, null, function(results) {
    searchResults(results, locationType)
});

Also there's no need to pass null if you don't have a data object, it's an optional parameter and jQuery checks if the second param is a function or not, so you can just do this:

$.getJSON(url, function(results) {
    searchResults(results, locationType)
});
Sign up to request clarification or add additional context in comments.

3 Comments

You must be careful with this. For a fixed value variable this is useful, but if you try to do this, for example, inside a loop where locationType is the index, its value inside the callback will be the value at the moment of the callback execution, not the value at definition. Just in case!
@DanibISHOP: And how would you pass a none fixed value?
non fixed values might be usefull if you do a lot of API calls and then still need to know which call originated from which ID you are loading
19

Warp in a function, e.g.

function getResults(locationType) {
    $.getJSON(url, null, function(results) {
        searchResults(results, locationType)
    });
}

But in you specific situation you don't even have to pass it, you can access the value directly in the callback.

1 Comment

if you are doing a loop and calling $.getJSON() each time, then wrapping it a function will preserve the variable; otherwise, it will be overridden.
4

You could use the .ajax method:

var locationType = 3;
var url = 'blah blah blah' + '&locationType=' + locationType;
$.ajax({
    url: url,
    context: { lt: locationType },
    success: function(results) {
        searchResults(results, this.lt);    
    }
});

2 Comments

I believe there is an error in the code above. When using the context setting, "this" becomes the context in the callback function, so the code should read searchResults(results, this.lt); instead of searchResults(results, this.context.lt);. I already made the correction above.
If this ajax call gets called multiple times (and possibly before the previous call returns), then this is the only correct solution.
2

If you want to use locationType (whose value is 3) in the callback, simply use

function(results) { .....

thanks to closures, locationType will be automatically available in the callback.

Comments

0

Could try:

function getResults(locationType) {
    $.getJSON(url, {p1:'xxx', p2: 'yyy'}, function(results) {
        searchResults(results, locationType)
    });
}

Comments

0
$.getJSON("url",
        "//here you can define your id, secure key to check data in your console, if you don't have condition for id and secure key just remove the secure key part."
        {secure_key:"noaccess"}, function(data){
            console.log(data);
});

Comments

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.