1

I'm trying to get an ajax request running, but I get a parsererror. This is my Javascript Code:

$.ajax({
            type: 'POST',
            url: 'insertuser.php',
            dataType: 'json',
            data: {
                nickname: $('input[name=nickname]').val(),
                passwort: $('input[name=passwort]').val()
            },
            success : function(data){
                console.log(data);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                console.log("XMLHttpRequest", XMLHttpRequest);
                console.log("textStatus", textStatus);
                console.log("errorThrown", errorThrown);    
            }
        });

Thats the php file:

$return['error'] = false;
$return['msg'] = "juhuuu";

echo json_encode($return);

This is the console.log:

XMLHttpRequest Object textStatus parsererror errorThrown SyntaxError

Thats what the .php echos: {"error":false,"msg":"juhuuu"}

I hope someone has an idea :)

4
  • sendt data by var str = $(this).serialize(); adn in php parse_str($_POST["data"],$array); Commented Oct 23, 2012 at 8:55
  • Did you try adding a content type header in your PHP like: header('Content-Type: application/json');? Commented Oct 23, 2012 at 9:40
  • dataType jsonp and the header thing don't work.. Commented Oct 23, 2012 at 10:59
  • 2
    Okay, I fixed it! :) I only had to remove all the HTML content in my php file, like <html>, <body>, <header> and so on... now it works! Commented Oct 23, 2012 at 11:23

1 Answer 1

0

This answer may or may not help somebody else. But I had a similar problem with a jQuery ajax json parse error. My form wouldn't send an email and was returning the parse error via jQuery.

jQuery

 $.ajax({
    type: "POST",
    url: "process-form.php",
    data: dataString,
    dataType:"json",
    success: function(response) {

        if(response.status == "success") {

        } else if (response.status == "error") {

        }
  });

PHP

if (empty($name) || empty($email) || empty($phone) || !preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/',$email) || !preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$phone)) {
            echo json_encode(array(
                'status' => 'error'
                //'message'=> 'error message'
            ));
        } else {
            $send = mail($sendTo, $subject, $message5, $headers, '‑[email protected]');
            $sendText = mail($sendToPhone, '', $txtmsg, 'From: example <[email protected]>\r\n');

            // insert into db
            $formName = $_POST['name'];
            $queryIn = "INSERT INTO database pseudo code";

            //execute the query. 
            $result = mysqli_query($connection, $queryIn);
            if (!$result) {
                printf("Error: %s\n", mysqli_error($connection));
                exit();
            }

        }
if($send){
            echo json_encode(array(
                'status' => 'success'
                //This is the message the .ajax() call was looking for but it never posted because there was a MySQL error being printed to the browser along with it. 
            ));
}

My issue was with my PHP page that the ajax call was going to (process-form.php). I was emailing the form data with the PHP mail() function and inserting the web form data into a MySQL Database. My PHP checks for mail() send success and prints the success message to the screen (which is what the jQuery .ajax() call is looking for. But my php/mysql was throwing an error and printing the error on the screen instead of the "success" message it was supposed to display. So my jQuery .ajax() call was trying to read a PHP json_encode() encoded array and it was getting the MySQL Error instead. Once I fixed my MySQL Error everything worked perfectly.

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

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.