对AJAX的成功没有警觉 [英] Not getting alert on success in AJAX
问题描述
这是我的AJAX代码。它工作正常,并将数据发送到我的数据库。但是,成功时我没有得到任何 alert()
。
This is my AJAX code. It's working fine and sends data to my database. However I don't get any alert()
on success.
你能告诉我原因吗?
$(function() {
$('#placeBid').on('submit', function(e) {
$.ajaxSetup({
headers: {
'X-CSRF-Token': $('meta[name="csrf_token"]').attr('content')
}
});
e.preventDefault(e);
$.ajax({
method:"POST",
url: $("#placeBid").attr("action"),
data: $(this).serialize(),
dataType: 'json',
success: function(data) {
if (data == true)
alert('working');
else
alert('not working');
},
error: function(data) {}
})
});
});
推荐答案
删除 dataType:' json'
选项将解决此问题,因为回调将等待数据
为 json
当你回来时我猜字符串
因为你试图创造一个条件 data == true
(由它永远不会实现,因为 data
返回的结果不能是boolean)。
Remove the dataType: 'json'
option will fix the issue because the callback will wait for data
to be json
when you're returning I guess string
because you're trying to make a condition data == true
(by the way it will be never achieved because data
returned could not be boolean).
所以只需删除选项并 dataType
默认情况下会生成的智能猜测(xml,json,script或html)
:
So just remove the option and dataType
will make by default Intelligent Guess for (xml, json, script, or html)
:
$.ajax({
method:"POST",
url: $("#placeBid").attr("action"),
data: $(this).serialize(),
success: function(data) {
if (data == 'true')
alert('working');
else
alert('not working');
},
error: function(data) {}
})
希望他的帮助。
这篇关于对AJAX的成功没有警觉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!