views:

388

answers:

3

Hi. I'm newbie with JQuery.

I got this function

function verify_at_bd(){                
    var u = "foo";
    var p = "bar";

    return $.post('auth.php', { name: u, password: p, mobile: '' },             
            function(result){         
                         return result;
                    },'json');   
}

If I do a console.log(verify_at_bd()) I'm getting an xmlhttprequest but cannot access to responseText property. I'm using header("Content-Type: application/json") into my PHP.

I'm using firefox 3.6 on OS X.

+1  A: 

Well, firstly, the XHR must be in readyState 4 to get responseText.

Secondly, it looks to me like you are abusing $.post(). It is an asynchronous call, you do not process the result of $.post(), you deal with the result in your success mehtod (function(result)).

If you really need the reponseText, then add a few more arguments to your success function and catch the xhr there.

See jquery docs for $.post to see which arg is the xhr.

Sky Sanders
A: 

As @code poet said, you're utilizing the $.post method in an awkward way. It is an asynchronous call, so you can't expect to see the result returned to you immediately until the AJAX POST request completes. If you could post the code you've written to handle the JSON you expect this function to return, it would be easier to advise you on a proper way to structure your $.post response.

bobthabuilda
A: 

Here goes:

$.ajax({
  type: 'POST',
  url: 'auth.php',
  data: {name: 'foo', password: 'bar', mobile: ''},
  success: function(result, textStatus, xmlhttprequest){
        console.log( $.parseJSON(xmlhttprequest.responseText).status);
    },
  dataType: 'json'
});

That's all.

Felix Guerrero