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的东西,但似乎没有任何结果。
4 个解决方案
#1
3
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
您的请求是错误的,如果您希望使用$ _POST超级全局,则不应发送json。将其作为常规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);
}
});
#2
2
Try to change the dataType to 'text' and contentType to 'application/json'
尝试将dataType更改为'text',将contentType更改为'application / json'
#3
1
You are "deleting" the post twice.
您正在“删除”该帖子两次。
Remove this line: $post->deletePost($deletePostID);
删除此行:$ post-> deletePost($ deletePostID);
#4
0
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 ..
为我工作过..
#1
3
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
您的请求是错误的,如果您希望使用$ _POST超级全局,则不应发送json。将其作为常规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);
}
});
#2
2
Try to change the dataType to 'text' and contentType to 'application/json'
尝试将dataType更改为'text',将contentType更改为'application / json'
#3
1
You are "deleting" the post twice.
您正在“删除”该帖子两次。
Remove this line: $post->deletePost($deletePostID);
删除此行:$ post-> deletePost($ deletePostID);
#4
0
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 ..
为我工作过..