dojo.xhrPost

Perkenalan dojo.xhrPost:

Fungsi dojo.xhrPost() adalah fungsi dasar lain dari pengembangan AJAX. Banyak kesamaan dengan temanya GET (dojo.xhrGET), bertujuan utk menyediakan interface yang mudah digunakan dan konisten utk membuat pemanggilan yang asynchronous. dojo.xhrPost diarahkan kepada pengiriman data ke server, paling sering dengan memposting data FORM, atau semacam/beberapa content body. Pada intinya, fungsi ini mengimplementasikan pembuatan sebuah asynchronous HTTP POST request.

Keterbatasannya:

– tidak dapat mengepost data binary. (pertimbangkan dojo.io.iframe)

– keterbatasannya sama dengan dojo.xhr.GET

Penggunaan:

Fungsi dojo.xhrPost() mengambil sebuah objek untk dijadikan sebagai parameternya. Objek ini menentukan bagaimana xhrPost seharusnya beroperasi. Semua parameter dojo.xhrGet adalah valid, termasuk bagaimana menset load and errors handlers. Jadi untuk informasi yang lebih detil silahkan mengarah ke dojo.xhrGet.

Contoh source code:

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>

<html dir=”ltr”>

<head>

<link rel=”stylesheet” type=”text/css” href=”http://ajax.googleapis.com/ajax/libs/dojo/1.5/dijit/themes/claro/claro.css”

/>

<style type=”text/css”>

body, html { font-family:helvetica,arial,sans-serif; font-size:90%; }

</style>

</head>

<body class=” claro “>

<b>

Simple Form:

</b>

<br>

<blockquote>

<form action=”postIt” id=”myform”>

Text:

<input type=”text” dojoType=”dijit.form.TextBox” name=”formInput” value=”Some text”>

</input>

<br>

<br>

Checkbox:

<input type=”checkbox” dojoType=”dijit.form.CheckBox” name=”checkboxInput”>

</input>

<br>

<br>

<button type=”submit” dojoType=”dijit.form.Button” id=”submitButton”>

Send it!

</button>

</form>

</blockquote>

<br>

<b>

Result

</b>

<div id=”response”>

</div>

</body>

<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/dojo/1.5/dojo/dojo.xd.js”

djConfig=”parseOnLoad: true”>

</script>

<script type=”text/javascript”>

dojo.require(“dijit.form.Button”);

dojo.require(“dijit.form.TextBox”);

dojo.require(“dijit.form.CheckBox”);

function sendForm() {

var button = dijit.byId(“submitButton”);

dojo.connect(button, “onClick”, function(event) {

//Stop the submit event since we want to control form submission.

event.preventDefault();

event.stopPropagation();

//The parameters to pass to xhrPost, the form, how to handle it, and the callbacks.

//Note that there isn’t a url passed. xhrPost will extract the url to call from the form’s

//’action’ attribute. You could also leave off the action attribute and set the url of the xhrPost object

//either should work.

var xhrArgs = {

form: dojo.byId(“myform”),

handleAs: “text”,

load: function(data) {

dojo.byId(“response”).innerHTML = “Form posted.”;

},

error: function(error) {

//We’ll 404 in the demo, but that’s okay. We don’t have a ‘postIt’ service on the

//docs server.

dojo.byId(“response”).innerHTML = “Form posted.”;

}

}

//Call the asynchronous xhrPost

dojo.byId(“response”).innerHTML = “Form being sent…”

var deferred = dojo.xhrPost(xhrArgs);

});

}

dojo.addOnLoad(sendForm);

</script>

</html>

Hasilnya :

../postIt?formInput=Ini++adalah+test…&checkboxInput=on