📜 ⬆️ ⬇️

Dojo: sending data

This time, let's figure out how to send data to the server using Dojo. Of course, it is fairly easy to get static data from the server, but in real life this is hardly useful, so now we will not only request data, but send some information to the server for processing. Now we need a working server with PHP, ASP, ColdFusion or Java.

Sending data to the server via Get-request


First, we will add an input field to our HTML code — also, of course, a widget. At first we had the following code:
<button dojoType = ”Button” widgetId = ”helloButton”>

<script type = "dojo / method" event = "onClick">
dojo.xhrGet ({
url: 'response.txt',
load: helloCallback,
error: helloError
});
</ script>
</ button>

We will replace it with this:
<button dojoType = ”dijit.form.Button” id = ”helloButton”>
Hello World!
<script type = "dojo / method" event = "onClick">
dojo.xhrGet ({
url: 'HelloWorldResponseGET.php',
load: helloCallback,
error: helloError,
content: {name: dojo.byId ('name'). value}
});
</ script>
</ button>
Enter your name: <input type = ”text” id = ”name”>

Before we continue, it should be noted that you will need to replace the url property in the dojo.xhrGet function in accordance with the server language that you will use. If you use ASP, you will need to replace HelloWorldResponseGET.php with HelloWorldResponseGET.asp, and so on, respectively.
Also note that a new property is passed to the function - content. It allows us to send some values ​​as parameters to the server. In our case, since we use the standard method dojo.io.bind - GET, then the value of the text field will be available to the server script via the name value in the GET request. Also, if the script would expect the myName parameter from us, and not the name, we would simply change the property in the content parameter from name to myName:
content: {myName: dojo.byId ('name'). value}

To find an object, we access it through the function dojo.byId with the parameter name of our object (the same as document.getElementById ()). Now, if you prepare the server script, enter your name in the input field and click the button, a window with the text “Hello, Vasya!” Will come out.
By the way, about server scripts. Here they are:

For PHP server:


<? php
/ *
* HelloWorldResponseGET.php
* /
header ('Content-type: text / plain');
print “Hi, {$ _GET ['name']}! \ n”;
?>

For an ASP server:


<%
'
'HelloWorldResponseGET.asp
'
response.ContentType = ”text / plain”
response.write (”Hi,” & request.querystring (”name”) & “! \ n”)
%>

For Perl:


#! / usr / bin / perl
#
# 'HelloWorldResponseGET.pl
use strict;
use CGI;
my $ cgi = CGI :: new ();
print $ cgi-> header (-type => “text / html; charset = utf-8?);
print “Hi,”. $ cgi-> param ('name'). “! \ N”;

Sending data to the server via Post-request


Using GET is, of course, good, but sometimes you need to transfer data from ordinary HTML forms. Dojo has the tools to simplify this task.

First, change our HTML code as follows. It was:
<br>
Please enter your name: <input type = "text" id = "name">

It became:
<br>
<form id = "myForm" method = "POST">
Please enter your name: <input type = "text" name = "name">
</ form>

Now change the dojo / method. It was:
<script type = "dojo / method" event = "onClick">
dojo.xhrGet ({
url: 'HelloWorldResponseGET.php',
load: helloCallback,
error: helloError,
content: {name: dojo.byId ('name'). value}
});
</ script>

It became:
<script type = "dojo / method" event = "onClick">
// Remember to change the URL!
dojo.xhrPost ({
url: 'HelloWorldResponsePOST.php',
load: helloCallback,
error: helloError,
form: 'myForm'
});
</ script>

We replaced dojo.xhrGet with dojo.xhrPost and removed the content property, replacing it with form. This tells Dojo that the data to be sent should be taken from the form whose id is myForm.
The result of the example is the same as in the previous example. Now server code:

For PHP:


<? php
/ *
* HelloWorldResponsePOST.php
* /
header ('Content-type: text / plain');
print “Hi, {$ _POST ['name']}! \ n”;
?>

For ASP:


<%
'
'HelloWorldResponsePOST.asp
'
response.ContentType = "text / plain"
response.write ("Hello," & request.form ("name") & "! \ n")
%>

For ColdFusion:


Hello, # form.name #, welcome to the world of Dojo!

For JSP:


<%
/ *
'HelloWorldResponsePOST.jsp
* /
response.setContentType ("text / plain");
%>
Hi, <% = request.getParameter ("name")%>!

For Perl:


#! / usr / bin / perl
#
# 'HelloWorldResponsePOST.pl
#
use strict;
use CGI;
my $ cgi = CGI :: new ();
print $ cgi-> header (-type => "text / html; charset = utf-8");
print "Hello,". $ cgi-> param ('name'). "! \ n";

')

Previous parts:


Part I
(crosspost from my blog kixlive.cn )

Source: https://habr.com/ru/post/37380/


All Articles