问题描述
我一直致力于项目中的删除帖功能。它在PHP中运行良好,但现在我想在Ajax中执行此操作,以防止刷新和所有。
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.
无论如何,当我执行我的ajax调用时,我收到一个错误:
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)
它表示此错误在第35行,第35行将我发送到
It says that this error is on line 35, line 35 sends me to
console.log(error);
无论如何,为了给你一个更好的观点,这是我的Ajax电话:
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();
});
});
我的deletePost.php代码:
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);
}
?>
我尝试了很多东西,比如更改dataType和contentType,但似乎没有任何结果。
I've tried many things like changing the dataType and contentType, but nothing seems to work out.
推荐答案
您的请求错误,如果您希望使用$ _POST超级全局,则不应发送json。将其作为常规网址编码表单数据发送
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);
}
});
这篇关于来自ajax调用的JSON输入的意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!