简体   繁体   English

来自 ajax 调用的 JSON 输入意外结束

[英]Unexpected end of JSON input from an ajax call

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.在 PHP 中一切正常,但现在我想在 Ajax 中这样做,以防止刷新等等。

Anyway, when I perform my ajax call, I get an error:无论如何,当我执行 ajax 调用时,出现错误:

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它说这个错误在第 35 行,第 35 行将我发送到

console.log(error);

Anyway, to give you a better view, here is my Ajax call:无论如何,为了让您更好地了解,这是我的 Ajax 调用:

$(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:还有我的 deletePost.php 代码:

<?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.我尝试了很多事情,比如更改 dataType 和 contentType,但似乎没有任何效果。

Your request is wrong, you should not be sending json if you expect to use the $_POST super global.您的请求是错误的,如果您希望使用 $_POST 超级全局,则不应发送 json。 Send it as regular url encoded form data将其作为常规 url 编码的表单数据发送

    $.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);
        }
    });

尝试将 dataType 更改为 'text' 并将 contentType 更改为 'application/json'

You are "deleting" the post twice.您“删除”了两次帖子。

Remove this line: $post->deletePost($deletePostID);删除这一行: $post->deletePost($deletePostID);

This is a very strange problem within frameworks and PHP in general, in your controller just:一般来说,这是框架和 PHP 中一个非常奇怪的问题,在您的控制器中:

echo(json_encode($status));

Has worked for me ..对我有用..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM