jQuery AJAX Post没有发布数据

Sil*_*ced 9 ajax jquery post

我已经放弃了,经过大约2个小时试图找出这个问题我已经陷入困境,帮助我解决堆栈溢出问题!

尝试将产品ID和数量发布到php文件,如果发现任何错误将返回错误

jQuery代码 -

$('span.add_to_cart').click(function () {
        $('div.cart').append('<span class="loading">LOADING</span>');
        $.ajax({
            type: "POST",
            url: "/onlineshop/scripts/add_to_cart.php",
            dataType: "json",
            data: { prod_id: $('input.product_id').val(), 
                    quantity: $('input.quantity').val()
            },
            contentType: "application/json",
            success: function(data) {
                $('span.loading').remove();
            },
            error: function(xhr, textStatus, thrownError, data) {
                alert("Error: " + thrownError);
                $('span.loading').remove();
            }
        })
    });
Run Code Online (Sandbox Code Playgroud)

我的PHP页面正在尝试 print_r($_POST)

它似乎没有发布我的数据,我尝试了很多不同的东西,都给我错误,说没有数据发布.

epi*_*isx 16

尝试更改contentTypeapplication/x-www-form-urlencoded或仅删除它,因为它是默认值.


Roc*_*mat 8

您的问题是您发布的数据与content-type您发送的数据不匹配.

contentType用于发送到服务器dataType的数据,用于从服务器接收的数据.

只需contentType: "application/json"从代码中删除即可.