jQuery AJAX Post 不发布数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8526569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:38:30  来源:igfitidea点击:

jQuery AJAX Post not posting data

jqueryajaxpost

提问by Silenced

Right I have given in, after about 2 hours trying to figure out this issue I've caved, help me out stackoverflow!

是的,我已经放弃了,经过大约 2 个小时试图找出我陷入困境的问题后,请帮助我解决 stackoverflow!

Trying to post a product id along with quantity to a php file that will return errors if it finds any

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

jQuery code -

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();
            }
        })
    });

And my PHP page is just trying to print_r($_POST)

我的 PHP 页面只是想 print_r($_POST)

It seems that it is not posting my data, I've tried many many different things, all give me the error saying that there is no data being posted.

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

回答by epignosisx

Try changing the contentTypeto application/x-www-form-urlencodedor just dropping it since it is the default value.

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

回答by Rocket Hazmat

Your problem is that the data you are posting is not matching the content-typeyou are sending.

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

contentTypeis for the data being sent to the server, and dataTypeis for the data being received from the server.

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

Just remove the contentType: "application/json"from your code.

只需contentType: "application/json"从您的代码中删除。