Vineet Kalghatgi
Vineet Kalghatgi

Reputation: 87

Body of post request is empty

The body of my jquery post request to my mongodb database is always empty for some reason. I've tried the same post request on other applications like insomnia and python on the terminal.

Hovewer, this html script which is in the same directory as that of the express server is unable to send a post request

code :

$("form").submit(function (event) {

            //getting the form data
            var input_values = $(this).serializeArray();
            var post_JSON = {};
            $.each(input_values, function (i, field) {
                post_JSON[field.name] = field.value;
            });
            console.log(JSON.stringify(post_JSON));

            //Post request to mongo db
            $.post("http://localhost:5000/users/add",                   // url
                JSON.stringify(post_JSON),                              // data to be submit

                function (data, status, jqXHR) {                        // success callback
                    console.log('status: ' + status + ', data: ' + data);
                },

                "json")
                .fail(function (xhr, status, error) {
                    console.log(xhr);
                });

the schema of user defined with mongoose schemas :

var UserSchema = new Schema({
    username: {
        type: String,
        required: true,
        trim: true,
    },
    password: {
        type: String,
        required: true,
    },
    favourites: [String],
    profile_img: Buffer,
    email: {
        type: String,
        match: /.+@.+/i, //Validates email ids
        unique: true,
        required: true,
    },
    search_history: [String],
    phone_number: {
        type: String,
        maxlength: 10,
        match: /^\d{10}$/,  //Validates phone number
    },
});

const User = mongoose.model('User', UserSchema);

module.exports = User;

here is the console error log : enter image description here

The json right before the bad post request errror is the actual data that I am attempting to send to the database. If I were to copy paste this json and try the post in insomnia, it works. But I need it to work here.

Upvotes: 1

Views: 425

Answers (2)

Nick Rameau
Nick Rameau

Reputation: 1318

You need to set your Content-type header to application/json to indicate the type of your data. SinceJquery.ajax makes this easier, I'd recommend you go with this instead:

$.ajax
({
    url: 'http://localhost:5000/users/add',
    type: 'post',
    data: post_JSON,
    contentType: 'application/json',
    dataType: 'json',
    success: function (data, status, jqXHR) {
        console.log('status: ' + status + ', data: ' + data);
    },
    error: function (xhr, status, error) {
        console.log(xhr);
    }
});

Upvotes: 2

Vijay Joshi
Vijay Joshi

Reputation: 959

You do not need to use stringify in data: JSON.stringify(post_JSON). Just send the object and jQuery will take care of the rest.

Upvotes: 1

Related Questions