Javascript Jquery ajax post请求不工作
我用ajax提交了一个简单的表单,但它总是给我一个错误。所有的错误都是“错误”。没有代码,没有描述。没有什么,当我在它失败时提醒它 带jQuery的Javascript:Javascript Jquery ajax post请求不工作,javascript,jquery,python,pyramid,Javascript,Jquery,Python,Pyramid,我用ajax提交了一个简单的表单,但它总是给我一个错误。所有的错误都是“错误”。没有代码,没有描述。没有什么,当我在它失败时提醒它 带jQuery的Javascript: $(document).ready(function(){ $(".post-input").submit(function(){ var postcontent = $(".post-form").val(); if (postcontent == ""){
$(document).ready(function(){
$(".post-input").submit(function(){
var postcontent = $(".post-form").val();
if (postcontent == ""){
return false;
}
$(".post-form").attr("disabled", "disabled");
$.ajax({
url: '/post',
type: 'POST',
data: {"post-form": postcontent},
dataType: json,
success: function(response, textStatus, jqXHR) {
alert("Yay!");
},
error: function(jqXHR, textStatus, errorThrown){
alert(textStatus, errorThrown);
}
});
});
});
HTML:
更新:
在使用firebug进行更多的调试之后,我发现post请求主体只包含post.submitted=post,而不是{“post form”:postcontent}的预期结果。我认为问题在于您传递的数据没有正确写入 尝试更改此选项:
data: {"post-form": postcontent},
为此:
data: 'post-form='+ $('.post-form').val(),
根据文档,您必须声明数据类型:
$.ajax({
type: 'POST',
url: url,
data: data,
success: success,
dataType: dataType
});
另外,查看服务器端代码,您实际上不想发布JSON格式的数据。这{“post form”:postcontent}
是JSON格式的数据。实际上,您要做的是发送文本或HTML。看起来是表单数据,我会猜测文本
试试这个:
$.ajax({
url: '/post',
type: 'POST',
data: 'post-form='+postcontent,
dataType: 'text',
success: function(response, textStatus, jqXHR) {
alert("Yay!");
},
error: function(jqXHR, textStatus, errorThrown){
alert(textStatus, errorThrown);
}
});
由于您正在发布
JSON
-数据,因此必须声明数据类型“JSON”:
这是一个javascript错误(客户端)、服务器错误还是python错误?@Hamish通过我的调试,我假设这是一个客户端错误。遗憾的是,这不起作用,但在使用firebug进行了更多的调试后,我发现post正文实际上是post。submitted=Post,而不是预期的Post form=postcontent。您真的想发布JSON还是只发布常规表单数据?@Wiz:您实际上可以删除整个表单。离开textarea和按钮,更改$(“.post input”).submit(到$(“.post submit”).click(function(){@Wiz:我能想到的只有在AJAX调用程序中使用CACHE:FALSE,请确保服务器端没有错误,因为这会引发错误(但应该是一个详细的错误,而不是“错误”)并尝试将完整的URL添加到您正在呼叫的页面。@Wiz:很高兴我能提供帮助。祝您的项目好运。
$.ajax({
type: 'POST',
url: url,
data: data,
success: success,
dataType: dataType
});
$.ajax({
url: '/post',
type: 'POST',
data: 'post-form='+postcontent,
dataType: 'text',
success: function(response, textStatus, jqXHR) {
alert("Yay!");
},
error: function(jqXHR, textStatus, errorThrown){
alert(textStatus, errorThrown);
}
});
$.ajax({
url: '/post',
type: 'POST',
dataType: "json",
data: {"post-form": postcontent},
success: function(response, textStatus, jqXHR) {
alert("Yay!");
},
error: function(jqXHR, textStatus, errorThrown){
alert(textStatus, errorThrown);
}