views:

200

answers:

1

edited: This is what i need:

sendpost = function(a,b,c){
    return jQuery.post('inc/operations.php', {a:b}, c, "json");
},

rotate = function(callback){
    //....
    alert(callback);
}

sendpost('operation', 'test', rotate)

old post: i use this function to return the response of post:

$.sendpost = function(){
    return jQuery.post('inc/operations.php', {'operation':'test'}, "json");
},

i want to make something like this:

in:

$.another = function(){
  var sendpost = $.sendpost();
  alert(sendpost);
}

but i get: [object XMLHttpRequest]

if i print the object with:

jQuery.each(sendpost, function(i, val) {
  $(".displaydetails").append(i + " => " + val + "<br/>");
});

i get:

details  abort => function () { x && h.call(x); g("abort"); }
dispatchEvent => function dispatchEvent() { [native code] }
removeEventListener => function removeEventListener() { [native code] }
open => function open() { [native code] }
setRequestHeader => function setRequestHeader() { [native code] }
onreadystatechange => [xpconnect wrapped nsIDOMEventListener]
send => function send() { [native code] }
readyState => 4
status => 200
getResponseHeader => function getResponseHeader() { [native code] }
responseText => mdaaa from php

how to return only the response in the variable?

+4  A: 

This is not possible.

AJAX calls are asynchronous, meaning that your code continues running before the server sends a reply.

When the return statement executes, there is no reply from the server yet.

It is possible to make a synchronous AJAX call, but it will completely freeze the browser and should be avoided at all costs.

Instead, you should make your function take a callback that will receive the server's response, and call that callback from $.post's callback. (This is how jQuery's AJAX functions return values)

EDIT: For example:

$.sendpost = function(callback) {
    return jQuery.post('inc/operations.php', {'operation':'test'}, callback, "json");
};

$.another = function() { 
    $.sendpost(function(response) {
        alert(response);
    });
};
SLaks
how i can make a callback to recive the server's response?can u post a little example? thanks!
robertdd
i receive this error in firebug: 'e.success.call is not a function'
robertdd
What exactly did you write?
SLaks
The callback parameter is the name of a function to be executed after the post function completes? i can put a name of a function there?
robertdd
You can pass an anonymous function, like I did, or the name of a regular function. (Without `()`, as that would _call_ the function)
SLaks
i made a function: $.myalert = function(callback){ alert(callback); }and i put myalert , $.myalert, $.myalert() @ the end of the post, but nopw, not working :(
robertdd
Please edit your question to include your entire code.
SLaks
i edited my question
robertdd
Change it to $.myAlert. Or, stop putting your functions inside $.
SLaks
robertdd
Pass the callback before the `"json"` argument, as in my edited example.
SLaks
:(( is not calling alert() function skips, but do the post call
robertdd
[It works for me](http://jsbin.com/okade3/edit). What version of jQuery are you using?
SLaks
jquery-1.4.2.min.js
robertdd
yep, my bad i forgot to json_encode in php :-" thank you!
robertdd