2 Replies - 1121 Views - Last Post: 01 May 2012 - 10:29 PM

#1 CarDriver   User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Question about $.post() function

Posted 01 May 2012 - 06:54 PM

Hey everyone, I just started messing around with jQuery a couple hours ago, and I think it's great. But I'm having some trouble understanding part of the $.post() function.

Here is some slightly modified example code that jQuery had on their site:
				$.post("test.php", {gpa: 4, sat: 1600},
					function(data) {
						alert(data);
					}
				);
I don't understand the function(data) part. What the heck is that called? A useless function? I've never seen a function declared without a name, but that's just me. An explanation would be appreciated.

Also, suppose I have a HTML form input box with the name "gpa" - how do I use jQuery to get the value submitted? I noticed a serialize() function, but I'm not sure if that would be applicable here (if it is, could I simply replace {gpa: 4, sat: 1600} with it?).

Edit: I tested that out, and it works. But I'm still very confused on the function(data) thing. Embedded functions as arguments freak me out.

Thanks!

This post has been edited by AVReidy: 01 May 2012 - 07:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Question about $.post() function

#2 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1695
  • View blog
  • Posts: 8,592
  • Joined: 07-September 06

Re: Question about $.post() function

Posted 01 May 2012 - 07:54 PM

They are known as closures, (not to be confused with Google's poorly named Closure javascript library). Basically, it is a function without a name, which can be stored in variables or passed as a function argument (this is what is happening in this case). jQuery's post expects a success and failure function (I believe, though it may just want a success function, not sure); both of which are closures and called (you guessed it) if the ajax call succeeds or fails.

Hopefully that makes sense.
Was This Post Helpful? 1
  • +
  • -

#3 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: Question about $.post() function

Posted 01 May 2012 - 10:29 PM

View PostBetaWar, on 02 May 2012 - 04:54 AM, said:

jQuery's post expects a success and failure function (I believe, though it may just want a success function, not sure); both of which are closures and called (you guessed it) if the ajax call succeeds or fails.

IIRC neither function is required by jQuery itself, but if there is no success callback function, no response processing will be possible.

and those callback functions are not Closures unless they use (closure) a variable from a parent scope. (e.g. the given example is not a Closure, just an anonymous function)

ex.
// stupid function, I know
function doSomething(callback)
{
    return callback();
}

function test1()
{
    return 1;
}

function test2(x)
{
    return x + 1;
}

// named callback
doSomething(test1);

// anonymous callback
doSomething(function() {
    return 2;
});

// Closure
var z = 4;
var test3 = function() {
    return test2(z);
};

// callback with explicit Closure
var y = 3;
doSopmething(function() {
    return test2(y);
});

// Callback with implicit Closure
z = 5;
doSomething(test4);

This post has been edited by Dormilich: 01 May 2012 - 10:38 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1