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
jQuery AJAX Post not posting data
提问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 contentType
to application/x-www-form-urlencoded
or just dropping it since it is the default value.
尝试更改contentType
为application/x-www-form-urlencoded
或只是删除它,因为它是默认值。
回答by Rocket Hazmat
Your problem is that the data you are posting is not matching the content-type
you are sending.
您的问题是您发布的数据与content-type
您发送的数据不匹配。
contentType
is for the data being sent to the server, and dataType
is for the data being received from the server.
contentType
用于发送到服务器dataType
的数据,用于从服务器接收的数据。
Just remove the contentType: "application/json"
from your code.
只需contentType: "application/json"
从您的代码中删除。