5

I'm trying to send a form to a PHP using $.ajax in jQuery. I send the whole thing as JSON, but, when I try to get the response, I get the 'parsererror'. What am I doing wrong?

jQuery fragment:

$("form").submit(function(){
    $.ajax({type: "POST",
        url: "inscrever_curso.php",
        data: {cpf : $("input#cpf").val(),nome : $("input#nome").val()},
        dataType: "json",
        success: function(data){
            alert("sucesso: "+data.msg);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            alert ("erro: "+textStatus);
        }
    });
    return false;
});

PHP:

<?php
    $return[msg]="Testing, testing.";
    echo json_encode($return);
?>
10
  • Thanks for the fast reply - I just added the quotes around msg in PHP and still get the error. :( Commented Aug 9, 2009 at 21:51
  • Oh :-( Are you sure it is "parse error" you get from the server ? Is your PHP script no longer than those two lines ? Because those look fine (no parse error, at least) ; you are using PHP >= 5.2, right ? Commented Aug 9, 2009 at 21:54
  • 2
    Shouldn't the answers starting with "Shouldn't" be comments against the original question and not use up answer space? Commented Aug 9, 2009 at 21:54
  • What do you get when you go to inscrever_curso.php in your web browser? Commented Aug 9, 2009 at 21:55
  • yeah, I was using a larger script before, but since it wasn't working, I tried to make it simple first. IT doesn't work even with this simple script. Commented Aug 9, 2009 at 21:55

4 Answers 4

6

I don't know if it's the cause of your problem, but here is one thing about your PHP code:

$return[msg]="Testing, testing.";
echo json_encode($return);

Doing this, with E_NOTICE error_reporting level, you get:

Notice: Use of undefined constant msg - assumed 'msg'

If notices are enabled and displayed on your server, is will cause an output that is not valid JSON (the ouput contains the error message before the JSON string).


To avoid that notice, you must use this syntax:

$return['msg']="Testing, testing.";
echo json_encode($return);

Note the quotes around msg: the key of the array-element you are creating is a string constant, so, it is enclosed in quotes.

If you don't put in these quotes, PHP searches for a defined constant called "msg", which probably doesn't exist.

For more information about this, you can take a look at this section of the manual: Why is $foo[bar] wrong?.

(I am not sure it'll solve your problem, but it's good to know anyway.)

Sign up to request clarification or add additional context in comments.

Comments

4

Your PHP code should probably be:

<?php
    $return = array();
    $return['msg']="Testing, testing.";
    echo json_encode($return);
?>

Comments

1

I just found out what was happening: I've got an .htaccess script for friendly URLs, and it was searching for the right PHP file, but in the wrong directory. I just added a '/' before the filename in the URL and then it worked.

1 Comment

Ergh :-D Nice job : we probably wouldn't have found this one ^^ Thanks for letting us know what caused the problem !
0

Not sure if this will help anyone but I just had the json_decode() call but without echo before it. Perfectly valid PHP but will mess up your AJAX response and cause the parsererror.

1 Comment

Well, if you don't echo anything then your response body will be empty and that is not valid JSON and will trigger the parse error in JS if you had set dataType to "json" or if your reply header has "Content-Type: application/json" set (as I found out today).

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.