0

i want to get a string from a php file, using a jquery post.

function getString(string) {
    return $.ajax({
        type : 'POST',
        url : 'scripts/getstring.php',
        data : { 'string': string }
    });
};

in the firebug console i can see that the desired string is found, but if i want to get it with

var blub = getString("test");
    alert(blub);

only "object Object" is shown. just cant get where my mistake is..

1

3 Answers 3

3

That Ajax request that is made to the server is performed asynchronously, so the ajax method actually returns an object representing the request itself, rather than the actual response from the server.

The jQuery XMLHttpRequest (jqXHR) object returned by $.ajax() as of jQuery 1.5 is a superset of the browser's native XMLHttpRequest object.

You could use the success callback instead:

function getString(string) {
    return $.ajax({
        type : 'POST',
        url : 'scripts/getstring.php',
        data : { 'string': string }
        success: function(result) {
            alert(result);
        },
    });
};

Or if you want to be a bit more flexible, you could take the callback function as a parameter:

function getString(string, callback) {
    return $.ajax({
        type : 'POST',
        url : 'scripts/getstring.php',
        data : { 'string': string }
        success: callback,
    });
};

getString('test', function(result) {
    alert(result);
})
Sign up to request clarification or add additional context in comments.

Comments

1

You are returning an jQuery jqXHR object.

If you want to deal with the data from the HTTP response, then you need to add a done (or success handler.

blub.done(function (data) { 
    alert(data);
});

Comments

0

object Object is the expected response, because the data being returned is and object.

If you want to see the resultant object, try:

console.log(blub) instead and view it in the console.

This can then help you determine the correct path to the data you want to retrieve in the object.

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.