0

how can I make mydata available in the success function below

function addWordAddress(mydata) {
    var url = 'http://site/';
    $.ajax({
        url: url,
        jsonpCallback: callbackName,
        jsonp: false,
        cache: true,
        dataType: "jsonp",
        success: function (json) {
            console.log(json);
            console.log(mydata);
        },
        error: function () {
            console.log("error");
        }
    });
}
Baz
  • 12,713
  • 38
  • 145
  • 268
  • it should already of access...mydata should be stored in the closure of the success function – compid Jul 30 '13 at 21:44
  • 1
    Should already be there, it's included. What is the result of that `console.log(mydata);` – tymeJV Jul 30 '13 at 21:46
  • The success callback closes over that variable, so that when the callback is called, it is still in scope. Perhaps `addWordAddress()`'s caller isn't passing what you expected? – Aaron Miller Jul 30 '13 at 21:47
  • Possible duplicate: http://stackoverflow.com/questions/2602981/jquery-how-to-pass-additional-parameters-to-success-callback-for-ajax-call – roland Jul 30 '13 at 21:47
  • 1. Make it global, or 2. Send it to backend and have backend echo it back (as part of json). – Majid Fouladpour Jul 30 '13 at 21:47
  • But what if the value of myData is different for each call of addWordAddress? Wont I see the state of myData for the last call of addWordAddress rather than its state when the particular callback was issued? – Baz Jul 30 '13 at 21:54
  • @Baz No, because you're inside of a scope. your code should work as-is. – Kevin B Jul 30 '13 at 21:59

0 Answers0