simbha

ajax-form-http-post-gae

Jul 1st, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. #https://siongui.github.io/2012/07/24/ajax-form-http-post-gae-python/
  2.  
  3. #app.yaml
  4.  
  5. application: ajax-form-post-gae-python
  6. version: 1
  7. runtime: python27
  8. api_version: 1
  9. threadsafe: true
  10.  
  11. handlers:
  12. - url: /
  13. static_files: index.html
  14. upload: index.html
  15.  
  16. - url: /post.js
  17. static_files: post.js
  18. upload: post.js
  19.  
  20. - url: /post
  21. script: post.application
  22.  
  23.  
  24. #post.py
  25.  
  26. #!/usr/bin/env python
  27. # -*- coding:utf-8 -*-
  28.  
  29. import webapp2
  30. import urllib2
  31.  
  32.  
  33. class PostPage(webapp2.RequestHandler):
  34. def post(self):
  35. input1 = urllib2.unquote(self.request.get('input1'))
  36. input2 = urllib2.unquote(self.request.get('input2'))
  37.  
  38. self.response.out.write("input1: %s\ninput2: %s" % (input1, input2))
  39.  
  40.  
  41. application = webapp2.WSGIApplication([
  42. ('/post', PostPage),
  43. ], debug=True)
  44.  
  45.  
  46. #post.js
  47.  
  48. /**
  49. * Cross-browser HTTP POST request
  50. * @param {string} url The url of HTTP POST request
  51. * @param {object} keyValuePairs The object which contains data of key-value
  52. * pair(s) to be POSTed. For example, object {'name': 'Bob',
  53. * 'age': '21'} represents "name=Bob&age=21".
  54. * @param {function} callback The callback function if HTTP POST succeeds
  55. * @param {function} failCallback The callback function if HTTP POST fails
  56. */
  57. HTTPPOST = function(url, keyValuePairs, callback, failCallback) {
  58. var xmlhttp;
  59.  
  60. if (window.XMLHttpRequest) {
  61. // code for IE7+, Firefox, Chrome, Opera, Safari
  62. xmlhttp=new XMLHttpRequest();
  63. } else {
  64. // code for IE6, IE5
  65. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  66. }
  67.  
  68. xmlhttp.onreadystatechange = function() {
  69. if (xmlhttp.readyState == 4) {
  70. if (xmlhttp.status == 200)
  71. callback(xmlhttp.responseText, url);
  72. else
  73. failCallback(url);
  74. }
  75. };
  76.  
  77. xmlhttp.open("POST", url, true);
  78. xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  79.  
  80. var kvpairs = '';
  81. for (var key in keyValuePairs) {
  82. if (kvpairs == '')
  83. kvpairs = encodeURIComponent(key) + '=' +
  84. encodeURIComponent(keyValuePairs[key]);
  85. else
  86. kvpairs = kvpairs + '&' + encodeURIComponent(key) + '=' +
  87. encodeURIComponent(keyValuePairs[key]);
  88. }
  89.  
  90. xmlhttp.send(kvpairs);
  91. };
  92.  
  93.  
  94. function ajaxformpost() {
  95. var keyValuePairs = {
  96. 'input1': document.getElementById('input1').value,
  97. 'input2': document.getElementById('input2').value
  98. };
  99.  
  100. var callback = function(responseText, url) {
  101. alert('responseText from url ' + url + ':\n'
  102. + responseText);
  103. };
  104.  
  105. var failCallback = function(url) {
  106. // write your own handler for failure of HTTP POST
  107. alert('fail to post ' + url);
  108. }
  109.  
  110. HTTPPOST("/post", keyValuePairs, callback, failCallback);
  111. }
  112.  
  113.  
  114. #index.html
  115.  
  116.  
  117. <!doctype html>
  118. <html>
  119. <head>
  120. <meta charset="utf-8">
  121. <title>AJAX HTML Form POST on Google App Engine Python example</title>
  122. </head>
  123. <body>
  124.  
  125. <form action="javascript:ajaxformpost();">
  126. <input id="input1" type="text" size='110'
  127. value="<example1>用 HashMap &lt Name, LinkedList &lt NameDistancePair &gt &gt 的 Java data structure 畫出此 directed weighted graph。</example1>">
  128. <br />
  129. <input id="input2" type="text" size='110'
  130. value="<example2>用 HashMap &lt Name, LinkedList &lt NameDistancePair &gt &gt 的 Java data structure 畫出此 directed weighted graph。</example1>">
  131. <br />
  132. <input type="submit" value="Submit">
  133. </form>
  134.  
  135. <script src="post.js"></script>
  136. </body>
  137. </html>
Add Comment
Please, Sign In to add comment