我有一个简单的单选按钮,用于使用jQuery .post异步提交数据到脚本。单击按钮时,脚本不会超过.post行,并且不会返回任何错误。
// this part submits the button click
$(document).ready(function() {
alert('test 1');
$("input:radio").change(function() {
alert('test 2');
$.post('http://google.com/', {'resolution':$(this).val()}, function(data) {
alert('test 3');
});
});
});
html在这里:
<input type='radio' id='radio1' name='resolution' checked/>
<input type='radio' id='radio2' name='resolution' />
还有一些额外的CSS用于完善这些单选按钮(使它们成为一个切换)。
半工作示例是here。
什么阻止.post行工作? (我应该看到一个弹出“test 3”)。
答案 0 :(得分:3)
您的Ajax帖子失败的原因有很多。
1)当我尝试使用Firebug调试你的帖子时,Google搜索(www.google.com)显然不接受发布数据。返回错误“405 Method not allowed”。这就是为什么你的帖子没有“成功”的原因。所以我将URL更改为www.imgur.com或任何其他接受帖子数据的URL。
2)要显示“测试3”,您要发布的服务器或URL必须发回一些数据。如果帖子仍然成功但服务器发送空响应,则不会出现警告消息“测试3”。尝试在服务器端回应一些内容。