PHP POST变量未从AJAX接收数据

PHP POST变量未从AJAX接收数据,php,json,ajax,post,Php,Json,Ajax,Post,我试图使用AJAX将元素的单击id发送到我的PHP文件,以便我的SQL语句可以选择适当的数据。我的$\u POST变量仍然未定义,一切似乎都正常 Javascript $(document).ready(function(){ $('.productsForm').submit(createEntry); $('.clickProducts').bind('click touchend', refreshEntries); }); function refreshEntries

我试图使用AJAX将元素的单击id发送到我的PHP文件,以便我的SQL语句可以选择适当的数据。我的
$\u POST
变量仍然未定义,一切似乎都正常

Javascript

$(document).ready(function(){
    $('.productsForm').submit(createEntry);
    $('.clickProducts').bind('click touchend', refreshEntries);
});

function refreshEntries() {
    //Get the id number of the clicked element.
    var clicked_id = (this.id);
    var cigar_id = clicked_id.toString();
    var display = "";
    $.ajax({
        url: 'php/mobile-select.php',
        type: 'POST',
        contentType: "application/json",
        dataType: 'json',
        data: { product_id : cigar_id
        },
        success: function(json){
            //I'm attempting to loop through the JSON object and create product arrays for each cigar.
            $.each(json, function(key, value){
               $.each(value, function(x, y){
                    display += '<tr><td>'+ x + '</td><td>' + y +'</td></tr>';
                });
            });

        console.log(display);


        }
    })
} 
$(文档).ready(函数(){
$('.productsForm')。提交(createEntry);
$('.clickProducts').bind('ClickTouchEnd',刷新条目);
});
函数refreshEntries(){
//获取单击的元素的id号。
var\u id=(this.id);
var cifigue_id=单击的_id.toString();
var display=“”;
$.ajax({
url:'php/mobile select.php',
键入:“POST”,
contentType:“应用程序/json”,
数据类型:“json”,
数据:{产品标识:雪茄标识
},
成功:函数(json){
//我试图循环遍历JSON对象,并为每根雪茄创建产品数组。
$.each(json、函数(键、值){
$。每个(值,函数(x,y){
显示+=''+x+''+y+'';
});
});
控制台日志(显示);
}
})
} 
PHP


您正在发送一个json对象,因此需要读取原始数据

$request_body = file_get_contents('php://input');
$post = json_decode($request_body, true);
$post将包含在数组中传递的对象


另外,在将product_id直接传递给SQL语句时也要小心。使您面临SQL攻击

我想我明白了。我不完全理解为什么,但是从我的.ajax函数中删除“contentType:”application/json”就成功了。非常感谢您的帮助。

您是否在浏览器控制台中观看了请求/响应?有什么错误吗?
$request_body = file_get_contents('php://input');
$post = json_decode($request_body, true);