单击按钮后,我想捕获表单的数据并使用 ajax、json 将其发送到 php 页面。现在,仅当我注释掉我的 AJAX 部分时,单击按钮才会出现“hello hello”警报。 当我通过取消注释来包含 ajax 部分时,我不仅没有收到任何错误,而且我的“你好你好”警报也没有出现。 我觉得这很奇怪。为什么会这样?
这是我的ajax:
<script>
$(document).ready(function () {
$("#btn").click( function() {
alert('hello hello');
/* Coomenting starts here
$.ajax({
url: "connection.php",
type: "POST",
data: {
id: $('#id').val(),
name: $('#name').val(),
Address: $('#Address').val()
}
datatype: "json",
success: function (status)
{
if (status.success == false)
{
alert("Failure!");
}
else
{
alert("Success!");
}
}
});
*/
//commenting ends here.
//basically i just commented out the ajax portion
});
});
</script>
最佳答案
“数据”后缺少逗号:
$.ajax({
url: "connection.php",
type: "POST",
data: {
id: $('#id').val(),
name: $('#name').val(),
Address: $('#Address').val()
}, // Missing comma!
datatype: "json",
success: function (status)
{
if (status.success == false)
{
alert("Failure!");
}
else
{
alert("Success!");
}
}
});
正如@dystroy 指出的那样,由于您的代码无法编译,这个格式错误的 block 可能会阻止其他代码(例如简单的警报)触发。
关于javascript - 非常基本的 jquery + ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11377778/