$ .ajax成功函数在我执行console.log(data)时不会显示在控制台中; [英] $.ajax success function won't show in the console when I do console.log(data);

查看:791
本文介绍了$ .ajax成功函数在我执行console.log(data)时不会显示在控制台中;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在$ .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() {}
});

解决方案

稍微更改您的代码以在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或其他错误.

In the $.ajax function I have the function for success like this:

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

but when I use the $.ajax function, it won't show anything in the console. If I use the $.post function and THEN I console.log(data), it WILL show up in the console, however it won't work because I can't have processData: false in the $.post function.

This is my entire $.ajax function:

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

解决方案

Slightly altering your code to run in jsfiddle, I don't see any problems (I get 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/

I suspect it's your URL. Watch Network tab in console to see if your server is returning a 404 or some other error.

这篇关于$ .ajax成功函数在我执行console.log(data)时不会显示在控制台中;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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