【问题标题】:$.ajax success function won't show in the console when I do console.log(data);当我执行 console.log(data); 时,$.ajax 成功功能不会显示在控制台中;
【发布时间】:2014-10-10 01:21:55
【问题描述】:

在 $.ajax 函数中,我有这样的成功函数:

success: function(data) {
    console.log(data);
},

但是当我使用 $.ajax 函数时,它不会在控制台中显示任何内容。如果我使用 $.post 函数,然后我使用 console.log(data),它将显示在控制台中,但它不会工作,因为我不能在 $.post 函数中有 processData: false。

这是我的整个 $.ajax 函数:

$.ajax({
    url: 'post',
    type: 'POST',
    processData: false,
    contentType: false,
    cache: false,
    dataType: 'json',
    data: formData,
    success: function(data) {
        console.log(data);
    },
    error: function() {}
});

【问题讨论】:

  • 你的目标 URL 确实叫 post 吗?
  • 你确定它会进入成功回调吗?似乎它可能会出错。在您的错误中添加警报以进行测试。
  • 在 Javascript 控制台中,有一个名为 Network 的选项卡。打开浏览器,打开控制台并进入其中的“网络”选项卡,然后打开您的页面并运行调用 AJAX 的代码。在“网络”选项卡中查找呼叫,看看会发生什么。您还可以将console.log() 添加到您的error 处理程序。
  • @karthikr 是的,我正在使用 laravel 并且 post 路由设置为 /user/post,现在我在 /user 中。好吧,我会测试一下。

标签: jquery ajax


【解决方案1】:

稍微改变您的代码以在 jsfiddle 中运行,我没有发现任何问题(我收到 Success! Object {}):

var formData = {test: 'test'};

$.ajax({
    url: '/echo/json',
    type: 'POST',
    processData: false,
    contentType: false,
    cache: false,
    dataType: 'json',
    data: formData,
    success: function(data) {
        console.log('Success!', data);
    },
    error: function(e) {
        console.log('Error!', e);
    }
});

http://jsfiddle.net/zta6ugyw/

我怀疑这是您的网址。观察控制台中的网络选项卡,查看您的服务器是否返回 404 或其他错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 1970-01-01
    • 2017-10-12
    • 2012-11-02
    相关资源
    最近更新 更多