1

Have a look at the following examples:

var data = "abc";   

$.post(ajaxurl,data,function(results) {
    console.log(data);
    console.log(results)
} ,'JSON');

data = "ghi";

Result:

data: "ghi"

results: "results"

Passing data:

var data = "abc";   

$.post(ajaxurl,data,function(results) {
    console.log(data);
    console.log(results)
} (data) ,'JSON');

data = "ghi";

Result:

data: "abc"

results: "abc"

However what I want to achieve is to pass the local variable data but recieve the passed variables from the callback anyway.

Desired output:

data: "abc"

results: "results"

In PHP this would be something like:

function abc($result) use ($data) {
    //...
}

How can I achieve this behaviour?

Edit - Clarification: I want to pass the current state of a local variable to an aynchronous request callback with arguments.

5
  • You want the value of the variable results to be the string 'results' but you never set it to that in the code? You also seem to want your AJAX callback to have access to the original value of data while you go off on modify data while the async op is completing? Am I getting that correctly? Commented Nov 28, 2017 at 13:55
  • @JaredSmith Yes this is almost correct. Do not care about results - passing of the callback result is automatically handled by jQuery. My problem is how can I pass in the current state of data such that it does not override results if passed in like in the second example. Commented Nov 28, 2017 at 13:57
  • Well guys I really do not understand why to close this the problem statement is really clear. I want to pass the current state of a local variable to an aynchronous request callback with arguments. Commented Nov 28, 2017 at 14:01
  • 1
    @Blackbam your edit made it clear, and I retracted my close vote and posted an answer. Commented Nov 28, 2017 at 14:06
  • Probably related JavaScript closure inside loops – simple practical example Commented Nov 28, 2017 at 14:13

2 Answers 2

3

Based on the conversation in the comments, you want this:

var data = "data";
$.post(ajaxurl,data,(function(data) { // shadows outer data
  return function(results) {
    console.log(data); // logs "data" not "foo"
    console.log(results); // logs the AJAX results
  };
})(data) ,'JSON');
data = "foo";

This will log "data" to the console rather than "foo" even though the async op completes later.

Sign up to request clarification or add additional context in comments.

Comments

2

You can use an IIFE to create a closure around the whole request. Assumes data is a primitive value. If it is array or object you either need to copy it or use promise callback to change it since arrays and objects are passed by reference not value

var data = "abc"; 

(function(data){    

    $.post(ajaxurl,data,function(results) {
     console.log(data); // abc
     console.log(results)
     } ,'JSON');

})(data);

data = "ghi";

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.