对AJAX的成功没有警觉 [英] Not getting alert on success in AJAX

查看:86
本文介绍了对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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆