2

I submit a form using jQuery to a php file on my server. Everything works... (the php file gets the right post variables, makes a database entry etc.) But on the response, sometimes 'data' goes wacky.

$('#form_submit').click( function() {
    $.post("path/to/script.php", $('#form').serialize(), function(data) {
        if ( data.status == 1 ) {
            alert('awesome sauce');
        } else {
            alert('crap');
        }
    }, "json");
});

php script returns (on success)

$response['status'] = 1;
$response['message'] = 'worked';
echo json_encode($response);
exit();

I'm getting a whole lot of crap, and not enough awesome sauce.

Does anyone have an idea why sometimes 'data.status' is undefined, and sometimes it isn't?

5
  • 1
    Maybe things go wrong in your PHP script. There is not really enough information to give a proper suggestion. Commented Sep 29, 2012 at 21:00
  • i'm using chrome. the thing that gets me is that i'll go grab a cup of coffee, come back and reload the page, and all of a sudden it'll work. Commented Sep 29, 2012 at 21:01
  • 3
    have you tried console.debug(data) ? This may provide insight into what you get at the times when you don't have data.status. Also, check the "network" tab, in chrome. Commented Sep 29, 2012 at 21:01
  • Felix, that's definitely enough info code wise. Commented Sep 29, 2012 at 21:01
  • i'll try that Will... and i've tried: {"status":1,"message":"worked"} ... WTF Commented Sep 29, 2012 at 21:02

2 Answers 2

3

Try it like this>

$('#form_submit').click( function() {
$.post("path/to/script.php", $('#form').serialize(), function(data) {
var obj = jQuery.parseJSON(data);
    if ( obj.status == 1 ) {
        alert('awesome sauce');
    } else {
        alert('crap');
    }
});
});
Sign up to request clarification or add additional context in comments.

10 Comments

yep I've checked that too but it seems that $.post is converting the JSON output directly to javascript object when it triggers the success event as they wrote in their API docs. (the second example from bottom in api.jquery.com/jQuery.post)
so the question i've got then... why is the data parsed correctly by jquery, and sometimes not. (when not using jQuery.parseJSON(data);)
i had same problems a day ago with json data and this part: var obj = jQuery.parseJSON(data); helped me
helped me too, but the unreliability of the other way of doing things is bothering me
You can explicitly pass in the dataType parameter to post(); it may be that jQuery is simply not guessing the expected return type properly.
|
0

try this one:

$('#form_submit').click( function() {
    $.post("path/to/script.php", $('#form').serialize())
     .success(function(){
        alert('awesome sauce');
     }).error(function(){
        alert('crap');
     });
});

1 Comment

yeah, that'll give me a workaround here where i'm guaranteeing that there's a success database-wise (data.status=1), but what if that fails (data.status=0)? this isn't reliable.

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.