|
0
|
1 |
|
|
|
2 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
|
|
3 |
<html> |
|
|
4 |
<head> |
|
|
5 |
<meta http-equiv="content-type" content="text/html; charset=utf-8"> |
|
|
6 |
<title>POST Transaction</title> |
|
|
7 |
|
|
|
8 |
<style type="text/css"> |
|
|
9 |
/*margin and padding on body element |
|
|
10 |
can introduce errors in determining |
|
|
11 |
element position and are not recommended; |
|
|
12 |
we turn them off as a foundation for YUI |
|
|
13 |
CSS treatments. */ |
|
|
14 |
body { |
|
|
15 |
margin:0; |
|
|
16 |
padding:0; |
|
|
17 |
} |
|
|
18 |
</style> |
|
|
19 |
|
|
|
20 |
<link type="text/css" rel="stylesheet" href="../../build/cssfonts/fonts-min.css" /> |
|
|
21 |
<script type="text/javascript" src="../../build/yui/yui-min.js"></script> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<!--begin custom header content for this example--> |
|
|
25 |
<style type="text/css"> |
|
|
26 |
#container li {margin-left:2em;} |
|
|
27 |
#container { background-color:#FFFFFF; border:1px dotted #666666; padding:1em; margin-bottom:1em;} |
|
|
28 |
</style> |
|
|
29 |
|
|
|
30 |
|
|
|
31 |
<!--end custom header content for this example--> |
|
|
32 |
|
|
|
33 |
</head> |
|
|
34 |
|
|
|
35 |
<body class=" yui-skin-sam"> |
|
|
36 |
|
|
|
37 |
<h1>POST Transaction</h1> |
|
|
38 |
|
|
|
39 |
<div class="exampleIntro"> |
|
|
40 |
<p>POSTing data to a server using YUI's IO utility is a simple process. Click "Send a POST Request" below to try it out, then read the description below to learn how it's done.</p> |
|
|
41 |
</div> |
|
|
42 |
|
|
|
43 |
<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
44 |
|
|
|
45 |
<div id="container"> |
|
|
46 |
<ul> |
|
|
47 |
<li>IO POST response data will appear here.</li> |
|
|
48 |
</ul> |
|
|
49 |
</div> |
|
|
50 |
<form><input type="button" id="requestButton" value="Send a POST Request"></form> |
|
|
51 |
|
|
|
52 |
<script> |
|
|
53 |
|
|
|
54 |
YUI({base:"../../build/", timeout: 10000}).use("io", |
|
|
55 |
|
|
|
56 |
function(Y) { |
|
|
57 |
|
|
|
58 |
//Get a reference to the Node that we are using |
|
|
59 |
//to report results: |
|
|
60 |
var div = Y.Node.get('#container ul'); |
|
|
61 |
|
|
|
62 |
//A function handler to use for successful requests: |
|
|
63 |
var handleSuccess = function(ioId, o){ |
|
|
64 |
Y.log(arguments); |
|
|
65 |
Y.log("The success handler was called. Id: " + ioId + ".", "info", "example"); |
|
|
66 |
|
|
|
67 |
if(o.responseText !== undefined){ |
|
|
68 |
var s = "<li>Transaction id: " + ioId + "</li>"; |
|
|
69 |
s += "<li>HTTP status: " + o.status + "</li>"; |
|
|
70 |
s += "<li>Status code message: " + o.statusText + "</li>"; |
|
|
71 |
s += "<li>HTTP headers received: <ul>" + o.getAllResponseHeaders() + "</ul></li>"; |
|
|
72 |
s += "<li>PHP response: " + o.responseText + "</li>"; |
|
|
73 |
div.set("innerHTML", s); |
|
|
74 |
} |
|
|
75 |
} |
|
|
76 |
|
|
|
77 |
//A function handler to use for failed requests: |
|
|
78 |
var handleFailure = function(ioId, o){ |
|
|
79 |
Y.log("The failure handler was called. Id: " + ioId + ".", "info", "example"); |
|
|
80 |
|
|
|
81 |
if(o.responseText !== undefined){ |
|
|
82 |
var s = "<li>Transaction id: " + ioId + "</li>"; |
|
|
83 |
s += "<li>HTTP status: " + o.status + "</li>"; |
|
|
84 |
s += "<li>Status code message: " + o.statusText + "</li>"; |
|
|
85 |
div.set("innerHTML", s); |
|
|
86 |
} |
|
|
87 |
} |
|
|
88 |
|
|
|
89 |
//Subscribe our handlers to IO's global custom events: |
|
|
90 |
Y.on('io:success', handleSuccess); |
|
|
91 |
Y.on('io:failure', handleFailure); |
|
|
92 |
|
|
|
93 |
|
|
|
94 |
/* Configuration object for POST transaction */ |
|
|
95 |
var cfg = { |
|
|
96 |
method: "POST", |
|
|
97 |
data: "user=YDN&password=API", |
|
|
98 |
headers: { 'X-Transaction': 'POST Example'} |
|
|
99 |
}; |
|
|
100 |
|
|
|
101 |
//The URL of the resource to which we're POSTing data: |
|
|
102 |
var sUrl = "assets/post.php"; |
|
|
103 |
|
|
|
104 |
//Handler to make our XHR request when the button is clicked: |
|
|
105 |
function makeRequest(){ |
|
|
106 |
|
|
|
107 |
div.set("innerHTML", "Loading data from new request..."); |
|
|
108 |
|
|
|
109 |
var request = Y.io(sUrl, cfg); |
|
|
110 |
Y.log("Initiating request; Id: " + request.id + ".", "info", "example"); |
|
|
111 |
|
|
|
112 |
} |
|
|
113 |
|
|
|
114 |
// Make a request when the button is clicked: |
|
|
115 |
Y.on("click", makeRequest, "#requestButton"); |
|
|
116 |
|
|
|
117 |
Y.log("As you interact with this example, relevant steps in the process will be logged here.", "info", "example"); |
|
|
118 |
} |
|
|
119 |
); |
|
|
120 |
</script> |
|
|
121 |
|
|
|
122 |
|
|
|
123 |
<!--END SOURCE CODE FOR EXAMPLE =============================== --> |
|
|
124 |
|
|
|
125 |
</body> |
|
|
126 |
</html> |