xhrPost
xhrPost data
Posted June 5th, 2008 by gayakrMy problem is very simple, just that I do not kow the answer since I am new to Dojo.
Requirement: Send fields from dijit.extended_dialog to the server.
Problem: 1) The form fields do not get submitted. The xhrPost returns the entire page.
Workaround: Can I send the fields using the 'content' sttribute of xhrPost ?
Would be really really really great if someone can help. Thank you
My Code is below:
Untitled Page
dojo.require("dijit.form.Button");
dojo.require("dojoc.Extended_Dialog");
dojo.require("dijit.Dialog");
dojo.require("dojo.parser");
dojo.require("dijit.form.TextBox");
function dataSubmit()
{
dojo.xhrPost
(
{
timeout: 5000,
url: "TAF_Final.aspx?smail='"+document.getElementById("sEmail").value+"'",
form: "tellAFriend",
handleAs: "text",
load: function(data)
{
if(typeof data == "error"){ alert ("Error");}
else
{
alert(data);
}
}
}
);
}
function init()
{
var theForm = dojo.byId("submitButton");
dojo.connect(theForm,"onclick","dataSubmit");
}
dojo.addOnLoad(init);
Sender E-mail:
Recipient E-mail:
Personal Message:
Your E-mail message
Dojo Example: xhrGet and xhrPost
Posted October 12th, 2007 by achilleanxhrGet
Description: The work-horse of the dojotoolkit, it allows you to send HTTP GET requests asynchronously.Use it anytime you need to grab information from the server asynchronously. The only exception is that you shouldn't use xhrGet for forms, use xhrPost!Example: http://www.dojoforum.com/demo-0.9/xhr/xhrGet.html
<head>
<title>Dojo xhrGet and xhrPost</title>
<!-- Initialize Dojo -->
<script src='/dojo-0.9/dojo/dojo.js' type='text/javascript'></script>
<script type='text/javascript'>
//<!--
// Function that retrieves the remote HTML and puts it into
// the <div> with an id of 'html-content'.
function getHtml () {
dojo.xhrGet ({
// Location of the HTML content we want to grab
url: '/demo-0.9/xhr/content.html',
// Called when the page loaded successfully
load: function (data) {
dojo.byId('html-content').innerHTML = data;
},
// Called if there was an error (such as a 404 response)
error: function (data) {
console.error('Error: ', data);
}
});
}
// Function that retrieves a JSON object and puts the information
// into the <div> with an id of 'json-content'. Notice how we're defining
// 'handleAs' to be of type 'json' now. That lets Dojo know that the data being
// retrieved from the URL should be eval()-ed and converted to a javascript object.
function getJson () {
dojo.xhrGet ({
url: '/demo-0.9/xhr/content.json',
handleAs: 'json', // IMPORTANT: tells Dojo to automatically parse the HTTP response into a JSON object
load: function (data) {
dojo.byId('json-content').innerHTML = data.content;
},
error: function (error) {
console.error('Error: ', error);
}
});
}
//-->
</script>
</head>
<body>
<div id='html-content'><a href='#' onClick='getHtml();'>Click me to get some HTML content</a></div>
<div id='json-content'><a href='#' onClick='getJson();'>Click me to get some JSON content</a></div>
</body>
</html>
The above code should be self-explanatory for the most part, though I did slip in a small function call that might be unfamiliar to people that have never seen Dojo: dojo.byId(). There's not much magic in the function, it's simply a much shorter alias for document.getElementById().
xhrPost
Description: It submits a HTTP POST request asynchronously. Use xhrPost when you want to send form data to a website and the form doesn't contain any file-input fields. (use dojo.io.iframe.send() instead)Example: http://www.dojoforum.com/demo-0.9/xhr/xhrPost.html
<head>
<title>Dojo Example: xhrGet and xhrPost</title>
<!-- Initialize Dojo -->
<script src='/dojo-0.9/dojo/dojo.js' type='text/javascript'></script>
<script type='text/javascript'>
//<!--
// The following function submits the data from the 'post-form' form
// to a PHP script located at
// 'http://www.dojoforum.com/demo-0.9/xhr/parse_form.php'
// The PHP script simply outputs a string in the format of
// 'Hello $name!', which is then put in the <div> w/ the id
// of 'response'.
//
// NOTE: As with xhrGet, you can also use handleAs to accept
// JSON objects in your load() function.
function submitForm() {
dojo.xhrPost ({
// The page that parses the POST request
url: '/demo-0.9/xhr/parse_form.php',
// Name of the Form we want to submit
form: 'post-form',
// Loads this function if everything went ok
load: function (data) {
// Put the data into the appropriate <div>
dojo.byId('response').innerHTML = data;
},
// Call this function if an error happened
error: function (error) {
console.error ('Error: ', error);
}
});
}
//-->
</script>
</head>
<body>
<div id='response'></div>
<div>
<form method='post' id='post-form'>
<input type='text' name='name' />
<a href='#' onClick='submitForm();'>Submit Form</a>
</form>
</div>
</body>
</html>
Most of the above code should look very familiar, as it's basically the same as xhrGet. In fact, xhrGet also supports the 'form' argument to its function call, but it's advised not to use it. If you want to see the source of the PHP file 'parse_form.php' then click here
I hope these brief examples have given you a glimpse into the most basic asynchronous XMLHttpRequest calls provided by the Dojo Toolkit. From here you should take a look at dojo.io.iframe.send(), dojox.io.xhrMultiPart() and for eye-candy dojox.widget.Loader.
Dojo Forum