我刚开始使用javascript和jquery。我使用jquery ajax,我很好奇为什么在dict中传递数据的一种方法有效但另一种方法没有。第二个例子有什么问题?
工作示例,我可以从dict发布数据
$('a.pop').click(function() {
var multi = parseInt($('#multiplier').val());
var prod = parseInt($('#producer').val());
$.ajax({
type: "POST",
url: "/myurl/post/",
data: {multiplier: multi, producer: prod},
success: function(data) {
bootbox.alert("operacja wykonana poprawnie");
},
beforeSend: function(xhr, settings){
xhr.setRequestHeader("X-CSRFToken", $.cookie('csrftoken'));
}
});
});
在/ myurl / post /中的这一个请求帖子是空的:
$('a.pop').click(function() {
$.ajax({
type: "POST",
url: "/myurl/post/",
data: {multiplier: parseInt($('#multiplier').val()), producer: parseInt($('#producer').val()), },
success: function(data) {
bootbox.alert($('#multiplier').val());
},
beforeSend: function(xhr, settings){
xhr.setRequestHeader("X-CSRFToken", $.cookie('csrftoken'));
}
});
});
答案 0 :(得分:-1)
$('a.pop').click(function() {
var multi = parseInt($('#multiplier').val());
var prod = parseInt($('#producer').val());
var obj = {
multiplier:multi ,
producer:prod
}
$.ajax({
type: "POST",
url: "/myurl/post/",
data: JSON.stringify(obj ,
contentType: "application/json; charset=utf-8",
success: function(data) {
bootbox.alert("operacja wykonana poprawnie");
},
beforeSend: function(xhr, settings){
xhr.setRequestHeader("X-CSRFToken", $.cookie('csrftoken'));
}
});
});
答案 1 :(得分:-1)
确保你的代码在document.ready中,因为在2个场景中它没有获得Id的值
// A $( document ).ready() Once the page loaded then your script will run
$( document ).ready(function() {
...
...
});