- The Book of Dojo
- Quick Installation
- Hello World
- Debugging Tutorial
- Introduction
- Part 1: Life With Dojo
- Part 2: Dijit
- Part 3: JavaScript With Dojo and Dijit
- Part 4: Testing, Tuning and Debugging
- Part 5: DojoX
- The Dojo Book, 0.4
Sending Form Data
Submitted by criecke on Tue, 06/05/2007 - 18:46.
The Url of dojo.xhrGet may contain parameters, like so:
url: 'myprogram.php?firstname=Chicken&lastname=Little&key=111111'
There are two problems: (1) it's difficult to URL encode everything, (2) it doesn't allow for dynamic parameters. The above works fine for everyone named Chicken Little, but ...
It's easier and more flexible to send an entire form of data. And you can do that with the form parameter of dojo.xhrGet
var kw = {
url: "myprogram.php",
load: function(data){
dojo.byId('myBox').value = data;
},
error: function(data){
console.debug("An error occurred: ", data);
},
timeout: 2000,
form: "myForm"
};
dojo.xhrGet(kw);
url: "myprogram.php",
load: function(data){
dojo.byId('myBox').value = data;
},
error: function(data){
console.debug("An error occurred: ", data);
},
timeout: 2000,
form: "myForm"
};
dojo.xhrGet(kw);
- Printer-friendly version
- Login or register to post comments
- Subscribe post
have more example?
thant is easy to trans it !!!
the form: "myform" can't is a dijit.form.Form?
In the Html page .
the "myform" if have a tag : dojoType="dijit.form.Form" ,
then in the xhr's from:"myform" can't works.
i have a wrong?
dijit.form.Form with Ajax
kwd={
url:"mypage.php",
load:function(response){ alert(response); return response; },
error:function (Response,ioArgs){
if (Response.dojoType=="cancel")
Message="Respuesta cancelada."
else if (Response.dojoType=="timeout")
Message="Respuesta fuera de tiempo."
else
Messaje=Response;
alert(ioArgs.xhr.status+' : '+Message);
},
timeout:5000,
content:AForm.getValues()
}
dojo.xhrPost(kwd);
'content' parameter is also still supported
Please also document the 'content' parameter - it is especially useful in posting large amounts of data as key-value pairs - not just form data. (This was also used in a previous comment).
Also a question: How do I set the content type in the HTTP request that goes to server?
handleAs
handleAs parameter sets the data type that the callback ('load:' parameter) will expect from the server. I've been mostly using 'json-comment-filtered' for json data.
I do not think you can set the data type of the request going to server. Just the response.
Anni
xhrPost
You can change xhrGet to xhrPost to have it perform a post, like when using password fields for example. It's obvious but I can't find it documented anywhere.
Submitting modal Dialogs via xhr
Hy,
after 2 days now I found an answer:
The dijit.Dialog acts the same way as a dijit.form.Form. So no form-Tag is needed, if one wants to submit a modal dialog via xhr, it is to handle the dijit.Dialog Id as form: in the xhr method.
Things can be so easy...
regards,
conni
Simple POST example
This is a simple example of a form post (with thanks to my predecessors).
HTML key notes
<input type="hidden" dojoType="dijit.form.TextBox" name="key" id="key" value="111111" />
<input type="text" dojoType="dijit.form.TextBox" name="firstname" id="firstname" length="50" />
<input type="text" dojoType="dijit.form.TextBox" name="lastname" id="lastname" length="50" />
<input type="text" dojoType="dijit.form.TextBox" id="myBox" name="myBox" length="50" />
</form>
<button onclick="postxhr();" >GO</button>
javascript key notes:
var kw = {
form: dojo.byId("myForm"),
url: "json.php",
handleAs:"json-comment-filtered",
load: function(response){
dijit.byId('myForm').setValues(response);
},
error: function(data){
alert("Holy Bomb Box, Batman! An error occurred: " + data);
},
timeout: 2000
};
//dojo.xhrGet(kw); //Servlet get argement with doGet
dojo.xhrPost(kw); //Servlet get argement with doPost
}
PHP key notes
echo <<<END
/*
{"firstname": "john",
"lastname": "doe"}
*/
END;
More examples please!
I would like to see more examples of URLs that work in a xhrGet() request.