Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST Request (Javascript)

Tags:

How do you make a simple POST request in Javascript without using a forms and without posting back?

like image 852
LB. Avatar asked Mar 28 '09 04:03

LB.


People also ask

What is POST request JavaScript?

post() method allows you to post data to the server in a single line. This is a simple wrapper for the more advanced $. ajax method. Below is an example of sending JSON to the ReqBin echo URL with jQuery Ajax method. JavaScript POST request with jQuery Ajax.

Can JavaScript handle POST request?

POST data is data that is handled server side. So there is no way you can read a post data using JavaScript.

What is meant by POST request?

By design, the POST request method requests that a web server accept the data enclosed in the body of the request message, most likely for storing it. It is often used when uploading a file or when submitting a completed web form. In contrast, the HTTP GET request method retrieves information from the server.

How do I do a POST request for an API?

To make a POST request to an API endpoint, you need to send an HTTP POST request to the server and specify a Content-Type request header that specifies the data media type in the body of the POST request. The Content-Length header indicates the size of the data in the body of the POST request.


2 Answers

Though I am taking the code sample from @sundeep answer, but posting the code here for completeness

var url = "sample-url.php"; var params = "lorem=ipsum&name=alpha"; var xhr = new XMLHttpRequest(); xhr.open("POST", url, true);  //Send the proper header information along with the request xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");  xhr.send(params); 
like image 175
Pankaj Avatar answered Oct 01 '22 00:10

Pankaj


You can do this using AJAX calls (XMLHttpRequest object)

http://www.openjs.com/articles/ajax_xmlhttp_using_post.php

like image 39
sundeep Avatar answered Oct 01 '22 02:10

sundeep