0

Below is the code to make a post request to node js api using ajax

$.ajax({
    url: "http://localhost:5555/submit",
    type: "POST",
    data :{formData: JSON.stringify(formdata)},
    dataType: "json",
    beforeSend: function(x) {
        if (x && x.overrideMimeType) {
            x.overrideMimeType("application/j-son;charset=UTF-8");
        }
    },
    success: function(data) {
        console.log("Data: " + data + "\nStatus: " + status);
        alert("Data: " + data + "\nStatus: " + status);
        location.reload();
    }         
});

NOde-js code to consume the above post request and perform functionalities:

app.post('/submit', function(req, res){
    console.log('request received by the submit endpoint');
    console.log(req.data);
    console.log(req.formData);
    var ipaddr;
    console.log(req);
});
3
  • 2
    checkout this answer stackoverflow.com/a/21662926/7096322 Commented Mar 19, 2018 at 23:51
  • 4
    Possible duplicate of Send form data using ajax Commented Mar 20, 2018 at 0:18
  • 1
    Note also that you may need to respond to the request by adding res.end() to your node function. Commented Mar 20, 2018 at 0:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.