Login Register

Sending Form Data

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);
<form id="myForm">
 <input type="hidden" name="key" value="111111" />
 <input type="text"  name="firstname" length="50" />
 <input type="text"  name="lastname" length="50" />
 
 <input type="text"  id="myBox" name="myBox" length="50" />
</form>

have more example?

	
	
	
	

	
myDiv
getxhr()< /button > function postxhr() { var kw = { url: "GetXHRPostData", handleAs:"text", load: function(response){ dojo.byId('myDiv').innerHTML = response; }, error: function(data){ alert("Holy Bomb Box, Batman! An error occurred: " + data); }, timeout: 2000, form: "myForm" }; //dojo.xhrGet(kw); //Servlet get argement with doGet dojo.xhrPost(kw); //Servlet get argement with doPost }

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

  • Set the dojoTypes
  • Ensure name and id are set
  • Button does not have to be in the form

<form name="myForm" id="myForm" dojoType="dijit.form.Form">
        <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:

  • dojo.byId on the form attribute
  • dijit.byId on the load

function postxhr() {
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

  • Header tag
  • Use of hereto echo to test syntax

header('Content-type: text/json-comment-filtered');
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.