Firefox和nodejs的jQuery Ajax POST错误

时间:2020-12-31 20:15:14

Im using nodejs and try to send a json to my server. The code works perfectly on chrome, but on firefox Im only get the error callback and nothing happens. I hope you guys have an idea. Im really desperate The console log:

我使用nodejs并尝试将json发送到我的服务器。代码在chrome上完美运行,但在firefox上我只得到错误回调而没有任何反应。我希望你们有个主意。我非常绝望控制台日志:

"readyState: 0"

"responseText: undefined"

"status: 0"

"text status: error"

“文字状态:错误”

"error: "

This is my app.js

这是我的app.js.

var db = require('./src/routes/dbconnection');
app.post('/db', db.postJson);

This is my script on the server

这是我在服务器上的脚本

exports.postJson = function (req, res) {
    var message_body = req.body;
    insertIntoDB(message_body);
};

var insertIntoDB = function (message_body) {
    var messages = require('mongoskin').db('localhost:27017', {
        database: 'messages'
    }).collection('header_message');

    messages.insert(message_body, function (err, result) {
        if (err) throw err;
        if (result) console.log('Added!');
    });
}

And this is my Ajax function

这是我的Ajax功能

$.ajax({
    type: 'POST',
    url: '/db',
    dataType: 'json',
    data: {
        color: color,
        icon: icon,
        message: banner_message,
        leftButtonText: leftButtonText,
        rightButtonText: rightButtonText,
        closeMeButtonOnly: closeMeButtonOnly,
        isEnabled: isEnabled
    },
    success: function () {
        givePopup_success();
        writeMessages();
        showCurrentOne();
        console.log("success");
    },
    error: function(xhr, textStatus, err) {
                console.log("readyState: " + xhr.readyState);
                console.log("responseText: " + xhr.responseText);
                console.log("status: " + xhr.status);
                console.log("text status: " + textStatus);
                console.log("error: " + err);
    }
}).complete(function (data) {
    console.log("done");
});

1 个解决方案

#1


0  

In your $.ajax options, use contentType: 'application/json' so the server knows what to expect in the data payload. It is better to be explicit.

在$ .ajax选项中,使用contentType:'application / json',以便服务器知道数据有效负载中的预期结果。最好是明确的。

Also, if you are using the dataType option, make sure your server always returns json in its response, even if it is just {}. If your server returns nothing, jQuery will think it is an error.

此外,如果您使用的是dataType选项,请确保您的服务器始终在其响应中返回json,即使它只是{}。如果您的服务器什么也没有返回,jQuery会认为这是一个错误。

For more info, read the dataType -> json section in the jQuery ajax docs.

有关更多信息,请阅读jQuery ajax文档中的dataType - > json部分。

#1


0  

In your $.ajax options, use contentType: 'application/json' so the server knows what to expect in the data payload. It is better to be explicit.

在$ .ajax选项中,使用contentType:'application / json',以便服务器知道数据有效负载中的预期结果。最好是明确的。

Also, if you are using the dataType option, make sure your server always returns json in its response, even if it is just {}. If your server returns nothing, jQuery will think it is an error.

此外,如果您使用的是dataType选项,请确保您的服务器始终在其响应中返回json,即使它只是{}。如果您的服务器什么也没有返回,jQuery会认为这是一个错误。

For more info, read the dataType -> json section in the jQuery ajax docs.

有关更多信息,请阅读jQuery ajax文档中的dataType - > json部分。