Php $\u AJAX请求后POST变量为空

Php $\u AJAX请求后POST变量为空,php,jquery,ajax,post,Php,Jquery,Ajax,Post,我已经看过很多关于这个问题的帖子,但其中许多帖子至少有3年了,似乎没有什么能解决我的问题。我希望有人能帮忙。我创建了一个字符串数组,这些字符串是服务器上文件的路径。我想将该数组传递给PHP文件,以取消指定文件的链接。问题是,当我使用jQuery的ajax函数将数组传递给PHP文件时,$\u POST变量为空,因此我无法处理数据。在本例中,URL很好,数组的长度为1。这是我的密码: jQuery $(".remove").click(function() { var images = do

我已经看过很多关于这个问题的帖子,但其中许多帖子至少有3年了,似乎没有什么能解决我的问题。我希望有人能帮忙。我创建了一个字符串数组,这些字符串是服务器上文件的路径。我想将该数组传递给PHP文件,以取消指定文件的链接。问题是,当我使用jQuery的ajax函数将数组传递给PHP文件时,$\u POST变量为空,因此我无法处理数据。在本例中,URL很好,数组的长度为1。这是我的密码:

jQuery

$(".remove").click(function() {
    var images = document.getElementsByTagName('img');
    var images2remove = [];

    $(images).each(function() {
        if($(this).hasClass('highlight')) {
            var path = $(this).attr('src');
            images2remove.push(path);
        }

    })

        $.ajax({
        url: 'removeFiles.php',
        type: 'post',
        data: {images : images2remove},  
        contentType: false,
        processData: false,
        success: function(response) {
            if(response != 0) {
              console.log(response);
            }
            else {
                alert('file not removed');
            }
        }
    })
});

还有我的php

$images = $_POST['images'];
var_dump($images);  // returns array(0){}

我也试过:

$post = file_get_contents('php://input'); 
var_dump($post) // returns string(15) [object, Object] 

我不太清楚该怎么办。根据我所看到的示例,我觉得应该能够使用$\u POST变量访问此数组的内容。我错过了什么?一如既往地谢谢你

看看这个小提琴的例子:

我必须将数据参数更改为json以符合异步调用

在PHP中

还有,看看这个

从AJAX调用中删除内容类型和流程数据

$.ajax({
        url: 'removeFiles.php',
        type: 'post',
        data: {images : images2remove},  
        success: function(response) {
            if(response != 0) {
              console.log(response);
            }
            else {
                alert('file not removed');
            }
        }
    })
内容类型:指示要发送的数据类型

流程数据:指示jquery应该序列化数据以将其发送到服务器


签出:

您在哪里看到$\u POST是空的?是从console.logresponse还是其他地方?另外,您的.remove HTML元素是什么?您没有阻止默认事件操作,因此如果是链接或提交按钮,您的页面可能正在导航。为什么有contentType:false和processData:false?将这些设置为false将导致problem@phil.remove只是一个没有链接的类型按钮。contentType:false是问题所在。简单的修复。非常感谢。我是从一篇我看过的文章中复制代码的,从来没有真正考虑过。我不建议让OP转到JSON。您所要说的就是删除contentType和processData选项json_decodestripslashes$_POST['json']在这个回答中是完全不必要的。我认为通过它发送原始POST数据不是一个好的做法。在这个问题上有很多不同的立场。。为了回答这个问题,我想说你是对的,上面有额外的代码。发布应用程序/x-www-form-urlencoded dataYes完全没有问题。删除内容类型和流程数据解决了我的问题。
$.ajax({
        url: 'removeFiles.php',
        type: 'post',
        data: {images : images2remove},  
        success: function(response) {
            if(response != 0) {
              console.log(response);
            }
            else {
                alert('file not removed');
            }
        }
    })