Nati323 Posted August 23, 2016 Share Posted August 23, 2016 Hey all, I want to send data to server using AJAX But I dont want to send it via post/get variables I want to send the data in the request body in JQuery I just do: $.ajax({ url: 'someurl', data: 'foo' }); But how can I do it in Pure JS ? Link to comment Share on other sites More sharing options...
dsonesuk Posted August 23, 2016 Share Posted August 23, 2016 But Im afraid the jquery example you provided appends data to the url and it is therefore sent as get request. Ajax by vanilla javascript must use either get or post. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 23, 2016 Share Posted August 23, 2016 You can just send data without any name/value pairs, any data you send will go in the request body. It will only get automatically parsed by something like PHP if it's in the name/value pair format. var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = ajax_handler; xhttp.open("POST", "script.php", true); xhttp.send('request body here');You may still want to URL-encode the request body. In PHP, for example, you would get the request body by reading from the php input stream. http://php.net/manual/en/wrappers.php.php 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now