2

I've got a jQuery AJAX code, which recieves JSON-type data from php file. Wihout dataType: "json" everything is ok. But I need JSON-type data. Text recieved is a valid JSON code

{"ok":"false","answer":"All fields must be filled"}

But when I user dataType, I've got an error

Object "parsererror" SyntaxError

Here's JS code:

    $.ajax({
        url : "testing/regtest.php",
        type : "POST",
        dataType: "json",
        data : {
            mail : $('#mail_field').val(),
            username : $('#username_field').val(),
            password : $('#password_field').val(),
            password_2 : $('#rep_password_field').val()
        },
        success : function(data) {
            console.log(data.ok);
        },
        error: function(a,b,c) { console.log(a,b,c); }
    });

Thanks for any help!
Update: Here's php server-side code: http://jsfiddle.net/VfQbz/1/
Update 2: It works in IE9 but doesn't work in chrome

5
  • 1
    Is the server-side script working properly? The JS itself looks fine. Commented Aug 24, 2011 at 21:15
  • Yep. Everything is ok with serverside Commented Aug 24, 2011 at 21:17
  • 1
    Where exacly does that error occur? Server or client-side? Commented Aug 24, 2011 at 21:18
  • 1
    Is the retrieved JSON valid? Already tried jsonlint.com ? Looks like a parsing problem to me Commented Aug 24, 2011 at 21:19
  • Client-side. And it occurs only with dataType set to jQuery. Retrieved JSON is valid. Commented Aug 24, 2011 at 21:19

3 Answers 3

3

I'm not quite sure if this works, but IMHO the problem is PHP's lousy JSON encoding function. Try:

if (check_post() === true) {
    $password = $_POST['password'];
    $password2 = $_POST['password_2'];
    $username = $_POST['username'];
    $mail = $_POST['mail'];
    if (valid_data($password, $password2, $username, $mail) === true){
        $answer = json_encode(array("ok" => "true", "answer" =>     $service_messages["account_registered"]));
        echo "'".$answer."'"; // Note the additional single quotes
    }
}

By digging into the jQuery source I found the error to occur in the response parsing. Hope it works.

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

Comments

1

Does your PHP code sets the correct header?

header('Content-type: application/json');

7 Comments

dataType: 'json' tells jquery to treat the returned string as json data, regardless of what the http headers say.
Well, I have this code $.getJSON(url, core.ProcessJsonResponse).error(core.ShowError); and this does not work without the Kohana::$content_type = 'application/json'; syntax. Only with content-type set I do not see HTML tab at firebug HTTP request result.
What does your PHP syntax looks like?
It works in IE and firefox on my system: img827.imageshack.us/img827/8241/stackoverflow.png
Did you try this in your succes? alert(data.ok + ":" + data.answer); Chrome 14.0.835.109 works!
|
1

Just to cover all the bases: are you sure your jQuery is up-to-date and not altered in any way?

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.