Unexpected end of JSON input from an ajax call

49,500

Solution 1

Your request is wrong, you should not be sending json if you expect to use the $_POST super global. Send it as regular url encoded form data

    $.ajax({
        url: "ajax/deletePost.php", 
        type: "POST",             
        data: {postDeleteID: postDeleteID},
        dataType: 'json',
        cache: false,
        success: function(data)
        {

        },
        error: function (request, status, error) {
            console.log(error);
        }
    });

Solution 2

Try to change the dataType to 'text' and contentType to 'application/json'

Solution 3

You are "deleting" the post twice.

Remove this line: $post->deletePost($deletePostID);

Share:
49,500
Albert.tomasiak
Author by

Albert.tomasiak

I'm a student at Thomas More Hogeschool. After school I work on my projects and work in our family business. I'm very interested in front and back end development, even though it's tricky at times.

Updated on April 17, 2021

Comments

  • Albert.tomasiak
    Albert.tomasiak about 3 years

    I've been working on a delete post functionality in my project. It all works fine in PHP, but now I'd like to do that in Ajax, to prevent the refresh and all.

    Anyway, when I perform my ajax call, I get an error:

    SyntaxError: Unexpected end of JSON input
    at Object.parse (native)
    at n.parseJSON (http://localhost/imdstagram/public/js/jquery-2.2.3.min.js:4:6401)
    at Ab (http://localhost/imdstagram/public/js/jquery-2.2.3.min.js:4:8347)
    at z (http://localhost/imdstagram/public/js/jquery-2.2.3.min.js:4:11804)
    at XMLHttpRequest.<anonymous> (http://localhost/imdstagram/public/js/jquery-2.2.3.min.js:4:15680)
    

    It says that this error is on line 35, line 35 sends me to

    console.log(error);
    

    Anyway, to give you a better view, here is my Ajax call:

    $(document).ready(function(){
    
        $(".post__delete").on("click", function(e){
            var postDeleteID = $('.deleteID').val();
    
            $.ajax({
                url: "ajax/deletePost.php", 
                type: "POST",             
                data: JSON.stringify(postDeleteID),
                dataType: 'json',
                contentType: false,
                cache: false,
                processData: false,
                success: function(data)
                {
    
                },
                error: function (request, status, error) {
                    console.log(error);
                }
            });
    
            e.preventDefault();
        });
    });
    

    And my deletePost.php code:

    <?php
        include_once("../classes/Post.class.php");
        session_start();
        $post = new Post();
    
        if(!empty($_POST)){
            $deletePostID = $_POST['deletePostID'];
    
            $post->deletePost($deletePostID);
    
            if($post->deletePost($deletePostID)){
                $status['delete'] = "success";
            } else {
                $status['delete'] = "failed";
            }
    
            header('Content-Type: application/json; charset=utf-8', true);
            echo json_encode($status);
        }
    
    ?>
    

    I've tried many things like changing the dataType and contentType, but nothing seems to work out.

  • Albert.tomasiak
    Albert.tomasiak about 8 years
    Thanks for that, deleted the line.
  • Albert.tomasiak
    Albert.tomasiak about 8 years
    Oh, you're right. I changed it, but I still get the same error.
  • Musa
    Musa about 8 years
    I forgot to take out the preocessData: false, try now
  • Albert.tomasiak
    Albert.tomasiak about 8 years
    Without the processData: false I get the following response: SyntaxError: Unexpected token < in JSON at position 0 at Object.parse (native) at n.parseJSON (localhost/imdstagram/public/js/jquery-2.2.3.min.js:4:6401) at Ab (localhost/imdstagram/public/js/jquery-2.2.3.min.js:4:8347) at z (localhost/imdstagram/public/js/jquery-2.2.3.min.js:4:11804) at XMLHttpRequest.<anonymous> (localhost/imdstagram/public/js/jquery-2.2.3.min.js:4:15680)
  • Musa
    Musa about 8 years
    That looks like you have html in you response.
  • Albert.tomasiak
    Albert.tomasiak about 8 years
    Thanks, I'll continue looking for the problem.