Make a POST with AJAX and JSON with pure Javascript [duplicate]

2

I have the following code in jQuery

$.post('/privmsg?', {
    folder: 'inbox',
    mode: 'post',
    post: '1',
    username: 'Wagner',
    subject: 'Título',
    message: 'Mensagem'
});

I'm studying Javascript, and would like to do the same with pure Javascript.

I have done some research and learned to make requests with GET with AJAX, now I would like to know, how do I do this POST with pure Javascript?

    
asked by anonymous 27.07.2014 / 21:22

1 answer

2

According to the site You Might Not Need jQuery , the syntax is this:

var request = new XMLHttpRequest();
request.open('POST', '/my/url', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.send(data);

Have this site in your favorites for whenever you want to do something without jQuery. Right on top you choose whether you want to see the IE8 +, IE9 + or IE10 + version.

The above version is compatible with IE8 +.

    
27.07.2014 / 21:30