-1

Good Day, I try to assign to a variable the result of an Ajax request. I tried both of the requests below, with no result (got an "Undefined" answer). I don't understand the difference between:

var temp = obj.method(me, you);

and

var temp = (function () {
  obj.method(me, you);
})();

Here is the AJAX request:

ObjID.prototype.getAjx = function(one, you){
   $.ajax({
        type: "post",
        url: "./php/getAJX.php",
        dataType: 'json',
        context: this,
        data: { one: one, two: two },
        success: function(data) {
            this.myProperty = data[0][0];
        },
        error:function(request, status, error) {
             console.log("Something went wrong." + request.responseText + status + error);
        } 
   });
}

Is this the same, or not ? Thanks for your help! :)

EricF
  • 181
  • 1
  • 3
  • 19
  • 3
    That can't be all your code. Where's the AJAX request? Please post the entire related code. – Scott Marcus Nov 10 '17 at 20:33
  • 1
    @ScottMarcus if this is a minimum viable and complete example, I'm fine with not seeing a wall of code. – stealththeninja Nov 10 '17 at 20:34
  • Yeah well the codes are different and do different things, but they behave equally. I bet this is another *return a response from an asynchronous call* problem – Jonas Wilms Nov 10 '17 at 20:34
  • So do I ........ – Teemu Nov 10 '17 at 20:35
  • what you're doing doesn't really make a lot of sense. to say `function temp(){ //your code }` is already assigning the function a name, functions in javscript are `first class citizens` and can be treated as a value like any other – Robbie Milejczak Nov 10 '17 at 20:35
  • 1
    @stealththeninja This is neither viable nor complete example. – JJJ Nov 10 '17 at 20:35
  • 1
    @stealththeninja Great for you. But, since the code provided does not show any AJAX calls, you'll be a little hard pressed to solve the issue. I didn't ask for all the code, just what was related. – Scott Marcus Nov 10 '17 at 20:36
  • @ScottMarcus The text refers to the "_result of AJAX_" though ..? – Teemu Nov 10 '17 at 20:37
  • @Teemu Yes, it does. What was your point? – Scott Marcus Nov 10 '17 at 20:37
  • 1
    https://stackoverflow.com/questions/111102/how-do-javascript-closures-work – Brent Barbata Nov 10 '17 at 20:39
  • @ScottMarcus After reading more carefully the comments, I suppose the same as yours = ). – Teemu Nov 10 '17 at 20:39
  • @BrentBarbata Why do you think closures are the problem here? – Scott Marcus Nov 10 '17 at 20:39
  • @ScottMarcus he just asked what the difference between the two is. – Brent Barbata Nov 10 '17 at 20:41
  • @RobbieMilejczak Your comment doesn't seem related to the question or the answer. You quote some code that was not even posted. – Scott Marcus Nov 10 '17 at 20:41
  • @BrentBarbata Yes. And, how are closures the answer to that question? – Scott Marcus Nov 10 '17 at 20:42
  • @ScottMarcus you're on a freakin crusade dude. more power to ya – Robbie Milejczak Nov 10 '17 at 20:42
  • @ScottMarcus is there another difference? – Brent Barbata Nov 10 '17 at 20:43
  • @RobbieMilejczak Just trying to help others who will come along later separate what's related and what's not. Closures are not the difference between the two. Functionally, there is no difference between the two. – Scott Marcus Nov 10 '17 at 20:43
  • @ScottMarcus well if I'm being totally honest I missed the part about the ajax call and misunderstood the nature of the question entirely but didn't delete my comment because it was marked a dupe – Robbie Milejczak Nov 10 '17 at 20:46
  • I updated the code above. I don't think having the Ajax request will help. All I want is to understand what's the difference, and what it does... – EricF Nov 10 '17 at 20:46
  • Now as more information has been provided, the answer is still "no" (assuming `obj.method === ObjID.getAjx`). – Teemu Nov 10 '17 at 20:46
  • `ObjID.prototype.getAjx` is not being assigned the result of your AJAX request. It's simply being assigned the function that, when executed, makes the AJAX request. Since that function doesn't contain any `return` statement(s), the function will return `undefined`. The AJAX request is `$.ajax()` and you aren't assigning the return value of that to anything. For that, you'd need to write something like `result = $.ajax({...})` – Scott Marcus Nov 10 '17 at 20:46
  • @Teemu I would vote to re-open the question as it turns out that it is not a dup of the other question, it's a mis-understanding of what the OP's function expression actually does. – Scott Marcus Nov 10 '17 at 20:51
  • @ScottMarcus Hmm... I'm not sure, the question is a bit ambiguous, even after the edits. "_I try to assign to a variable the result of an Ajax request_" says that the dup stands ..? – Teemu Nov 10 '17 at 20:55
  • @Teemu Yes, but look at his code. He's not actually doing that. He's trying to get the return value of the function that contains the AJAX call. And, this isn't the classic attempt to get a result before the asynchronous operation has completed (which is what the dup question is about). He's got the correct `success` callback in place. It's about assigning the result of the AJAX call itself, which would be a Promise. – Scott Marcus Nov 10 '17 at 20:57
  • @Teemu From **[JQuery Docs](http://api.jquery.com/jquery.ajax/)**: *"The jqXHR objects returned by $.ajax() as of jQuery 1.5 implement the Promise interface, giving them all the properties, methods, and behavior of a Promise (see Deferred object for more information)."* – Scott Marcus Nov 10 '17 at 21:00
  • @ScottMarcus Well .. The question is, whether the the two snippets are the same ... _If_ `obj.method` refers to `ObjID.prototype.getAjx`, the answer is clearly "no" (as I've stated in an earlier comment), and it really has nothing to do with AJAX being async ... I think you're right with this, and we (who voted to close as a dup) should retract the dup vote. – Teemu Nov 10 '17 at 21:06
  • @Teemu Yes, that was the question asked in the body of the question, but it was based on a false premise that the result of the call was being stored, which it isn't and that both syntaxes return `undefined`. So, to me, it makes the rest of the question off-topic. – Scott Marcus Nov 10 '17 at 21:08
  • @Alexandru-IonutMihai After OP's edits. It looks like we've picked at least a wrong dup target, please consider re-hammering the question. – Teemu Nov 10 '17 at 21:10

1 Answers1

1

The first two bits of code you showed effectively do the same thing.

However, neither of them do what you think they do. Neither of them are assigning the return value from your AJAX call anywhere:

The first line of your code:

ObjID.prototype.getAjx = function(one, you){

is simply assigning the function that contains the actual AJAX call to the .getAjx property of the ObjID.prototype. That function doesn't contain any return statements and upon execution, will not return anything. That is why you are getting undefined.

The code that actually performs the AJAX call is:

 $.ajax({
      type: "post",
      url: "./php/getAJX.php",
      dataType: 'json',
      context: this,
      data: { one: one, two: two },
      success: function(data) {
          this.myProperty = data[0][0];
      },
      error:function(request, status, error) {
           console.log("Something went wrong." + request.responseText + status + error);
      } 
 });

and, you aren't assigning that return value to anything. If you wanted to, you'd need to write something like this:

 var result = $.ajax({
      type: "post",
      url: "./php/getAJX.php",
      dataType: 'json',
      context: this,
      data: { one: one, two: two },
      success: function(data) {
          this.myProperty = data[0][0];
      },
      error:function(request, status, error) {
           console.log("Something went wrong." + request.responseText + status + error);
      } 
 });

JQuery AJAX calls return a reference to a Promise object and that's what you would be storing in that case.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • Well, THANKS Scott! That's very helpful. Obviously, you managed to understand what I needed :) – EricF Nov 10 '17 at 23:00