Sending an request to server to fetch data is very common requirement in applications. With dojo, it is very easy to send Ajax POST request where you submit some information to the server. The data is submitted in the body of HTTP POST request. This data could be the form elements or hard coded values to be sent along with the request.

In vanilla javascript, one would use XMLHttpRequest and then wait for change in state which is in-convenient when we have to send a lot of POST Ajax requests. But with dojo, it is just a matter of invoking a method and passing the required parameters to it.

The following code sample shows how to invoke a POST request using Ajax in Dojo:

		dojo._base.xhr.post({
				url: '/url',
				content: {
					param1 : 'val1',
					param2 : 'val2'
				},
				load: function(response) {
					dijit.byId('response').innerHTML=response;
				},
				error : function(response) {
					dijit.byId('response').innerHTML=response;
				}
                 });

In the above code, we don’t have to handle any change in state or need to explicitly send the request. We simply use the xhr.post method of Dojo to get the home page of google.

I have added the above code as a jsfiddle so that you can see it working. You may also edit the code of jsfiddle and experiment with it.

URL

Note: Almost all browsers have the limitation of not sending cross-domain ajax request. It is true with our example also. But if you really want cross domain ajax requests with dojo then you can try experimenting with dojo.io.frame.

The official dojo xhr post page is located at: http://dojotoolkit.org/reference-guide/1.8/dojo/xhrPost.html

Parameters

Other ways to send data with ajax post request is by using the form or postData parameters. The usage scenario of all parameters is explained below:

form : specify the dom id of the form element which you want to submit. All its child HTML components are submitted with id as key.

content : this parameters is used to send any custom built key value pair data which may or may not utilize web page HTML components

postData : if the requirement is sending a string only and no key/value pairs then use the postData parameter

Caution : All the three paramaters are optional so you may skip all of them. But only one of them has to be used in a Ajax request else you will get a javascript error.

Response handlers

The load and error parameters define what to do when a success response or an error is returned from the server. The error could be any HTTP error like 404, 501 etc.
You can add any javascript code in the body of these response/error handlers.

How to send post request in dojo admin Dojo
Sending an request to server to fetch data is very common requirement in applications. With dojo, it is very easy to send Ajax POST request where you submit some information to the server. The data is submitted in the body of HTTP POST request. This data could be the...
<p>Sending an request to server to fetch data is very common requirement in applications. With dojo, it is very easy to send Ajax POST request where you submit some information to the server. The data is submitted in the body of HTTP POST request. This data could be the form elements or hard coded values to be sent along with the request.</p> <p>In vanilla javascript, one would use XMLHttpRequest and then wait for change in state which is in-convenient when we have to send a lot of POST Ajax requests. But with dojo, it is just a matter of invoking a method and passing the required parameters to it.</p> <img src="http://www.javaexperience.com/wp-content/uploads/2012/09/dojo-xhr-post.png" alt="" title="dojo xhr post" width="396" height="35" class="alignnone size-full wp-image-271" /> The following code sample shows how to invoke a POST request using Ajax in Dojo: 1 <p>In the above code, we don't have to handle any change in state or need to explicitly send the request. We simply use the xhr.post method of Dojo to get the home page of google. I have added the above code as a <a href="http://jsfiddle.net/EL3uW/1/">jsfiddle</a> so that you can see it working. You may also edit the code of jsfiddle and experiment with it. </p> <h3>URL</h3> <p> Note: Almost all browsers have the limitation of not sending cross-domain ajax request. It is true with our example also. But if you really want cross domain ajax requests with dojo then you can try experimenting with dojo.io.frame. The official dojo xhr post page is located at: http://dojotoolkit.org/reference-guide/1.8/dojo/xhrPost.html </p> <h3>Parameters</h3> <p> Other ways to send data with ajax post request is by using the form or postData parameters. The usage scenario of all parameters is explained below: <strong>form :</strong> specify the dom id of the form element which you want to submit. All its child HTML components are submitted with id as key. <strong>content :</strong> this parameters is used to send any custom built key value pair data which may or may not utilize web page HTML components <strong>postData :</strong> if the requirement is sending a string only and no key/value pairs then use the postData parameter Caution : All the three paramaters are optional so you may skip all of them. But only one of them has to be used in a Ajax request else you will get a javascript error. </p> <h3>Response handlers</h3> <p> The load and error parameters define what to do when a success response or an error is returned from the server. The error could be any HTTP error like 404, 501 etc. You can add any javascript code in the body of these response/error handlers. </p>