Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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 :)

share|improve this question
    
Try dataType : "jsonp" – Burimi Oct 23 '12 at 8:52
    
sendt data by var str = $(this).serialize(); adn in php parse_str($_POST["data"],$array); – NullPoiиteя Oct 23 '12 at 8:55
    
Did you try adding a content type header in your PHP like: header('Content-Type: application/json');? – migg Oct 23 '12 at 9:40
    
dataType jsonp and the header thing don't work.. – user1541220 Oct 23 '12 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! – user1541220 Oct 23 '12 at 11:23

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.