ajax.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. module('core.ajax');
  2. var ajax_request_baseurl = upath + 'ajax.php';
  3. test("post请求,无数据", function () {
  4. UE.ajax.request(ajax_request_baseurl, {
  5. onsuccess: function (xhr) {
  6. equals(xhr.responseText, "", "post请求,无数据");
  7. start();
  8. },
  9. onerror: function () {
  10. ok(false, 'fail to send ajax request');
  11. start();
  12. }
  13. });
  14. stop();
  15. });
  16. test("get请求,无数据,url中有数据", function () {
  17. UE.ajax.request(ajax_request_baseurl + "?get1=ueditor&get2=baidu", {
  18. method: 'GET',
  19. onsuccess: function (xhr) {
  20. equals(xhr.responseText, "get1='ueditor'&get2='baidu'", "post请求,数据放在url中传递");
  21. start();
  22. },
  23. onerror: function () {
  24. ok(false, 'fail to send ajax request');
  25. start();
  26. }
  27. });
  28. stop();
  29. });
  30. test("get请求,有数据,url中有数据", function () {
  31. UE.ajax.request(ajax_request_baseurl + "?get1=ueditor&get2=baidu", {
  32. method: 'GET',
  33. content: "img1=http://www.baidu.com&img2=http://ueditor.baidu.com",
  34. onsuccess: function (xhr) {
  35. equals(xhr.responseText, "get1='ueditor'&get2='baidu'&img1=http://www.baidu.com&img2=http://ueditor.baidu.com", "post请求,数据放在url中传递");
  36. start();
  37. },
  38. onerror: function () {
  39. ok(false, 'fail to send ajax request');
  40. start();
  41. }
  42. });
  43. stop();
  44. });
  45. test("get请求,有data字段,无数据,url中有数据", function () {
  46. UE.ajax.request(ajax_request_baseurl + "?get1=ueditor&get2=baidu", {
  47. method: 'GET',
  48. data: {
  49. img1: 'http://www.baidu.com', img2: 'http://www.google.com'
  50. },
  51. onsuccess: function (xhr) {
  52. equals(xhr.responseText, "get1='ueditor'&get2='baidu'&img1='http://www.baidu.com'&img2='http://www.google.com'", "post请求,数据放在url中传递");
  53. start();
  54. },
  55. onerror: function () {
  56. ok(false, 'fail to send ajax request');
  57. start();
  58. }
  59. });
  60. stop();
  61. });
  62. test("post请求,有data字段", function () {
  63. UE.ajax.request(ajax_request_baseurl, {
  64. data: {
  65. img1: 'http://www.baidu.com', img2: 'http://www.google.com'
  66. },
  67. onsuccess: function (xhr) {
  68. equals(xhr.responseText, "img1='http://www.baidu.com'&img2='http://www.google.com'", "post请求,有data字段");
  69. start();
  70. },
  71. onerror: function () {
  72. ok(false, 'fail to send ajax request');
  73. start();
  74. }
  75. });
  76. stop();
  77. });
  78. test("post请求,没有data字段,有其他数据", function () {
  79. UE.ajax.request(ajax_request_baseurl, {
  80. content: "img1=http://www.baidu.com&img2=http://ueditor.baidu.com",
  81. onsuccess: function (xhr) {
  82. equals(xhr.responseText, "img1=http://www.baidu.com&img2=http://ueditor.baidu.com", "没有data字段,有其他数据");
  83. start();
  84. },
  85. onerror: function () {
  86. ok(false, 'fail to send ajax request');
  87. start();
  88. }
  89. });
  90. stop();
  91. });
  92. test("post请求,有data字段,有其他数据", function () {
  93. UE.ajax.request(ajax_request_baseurl, {
  94. data: {
  95. img1: 'http://www.baidu.com', img2: 'http://www.google.com'
  96. },
  97. content: "i1=http://www.baidu.com&i2=http://ueditor.baidu.com",
  98. onsuccess: function (xhr) {
  99. equals(xhr.responseText, "img1='http://www.baidu.com'&img2='http://www.google.com'&i1=http://www.baidu.com&i2=http://ueditor.baidu.com", "有data字段,有其他数据");
  100. start();
  101. },
  102. onerror: function () {
  103. ok(false, 'fail to send ajax request');
  104. start();
  105. }
  106. });
  107. stop();
  108. });
  109. test("get请求,有data字段,有其他数据", function () {
  110. UE.ajax.request(ajax_request_baseurl, {
  111. method: 'GET',
  112. data: {
  113. get1: 'http://www.baidu.com', get2: 'http://www.google.com'
  114. },
  115. content: "i1=http://www.baidu.com&i2=http://ueditor.baidu.com",
  116. onsuccess: function (xhr) {
  117. equals(xhr.responseText, "get1='http://www.baidu.com'&get2='http://www.google.com'&i1=http://www.baidu.com&i2=http://ueditor.baidu.com", "有data字段,有其他数据");
  118. start();
  119. },
  120. onerror: function () {
  121. ok(false, 'fail to send ajax request');
  122. start();
  123. }
  124. });
  125. stop();
  126. });
  127. test("并发多个post请求", function () {
  128. UE.ajax.request(ajax_request_baseurl, {
  129. data: {
  130. img1: 'http://ueditor.baidu.com', img2: 'http://www.google.com'
  131. },
  132. content: "i1=http://www.baidu.com&i2=http://ueditor.baidu.com",
  133. onsuccess: function (xhr) {
  134. equals(xhr.responseText, "img1='http://ueditor.baidu.com'&img2='http://www.google.com'&i1=http://www.baidu.com&i2=http://ueditor.baidu.com", "有data字段,有其他数据");
  135. },
  136. onerror: function () {
  137. ok(false, 'fail to send ajax request');
  138. }
  139. });
  140. UE.ajax.request(ajax_request_baseurl, {
  141. data: {
  142. img1: 'http://map.baidu.com', img2: 'http://www.google.com'
  143. },
  144. content: "p1=http://www.baidu.com&p2=http://ueditor.baidu.com",
  145. onsuccess: function (xhr) {
  146. equals(xhr.responseText, "img1='http://map.baidu.com'&img2='http://www.google.com'&p1=http://www.baidu.com&p2=http://ueditor.baidu.com", "有data字段,有其他数据");
  147. start();
  148. },
  149. onerror: function () {
  150. ok(false, 'fail to send ajax request');
  151. start();
  152. }
  153. });
  154. stop();
  155. });
  156. test("jsonp请求,无数据", function () {
  157. UE.ajax.request(ajax_request_baseurl, {
  158. dataType: 'jsonp',
  159. onsuccess: function (r) {
  160. notDeepEqual(r, null, '返回内容不为空');
  161. notEqual(r.callback, null, '返回内容有callback参数');
  162. start();
  163. },
  164. onerror: function () {
  165. ok(false, 'fail to send jsonp request');
  166. start();
  167. }
  168. });
  169. stop();
  170. });
  171. test("jsonp请求,无数据,url上有数据", function () {
  172. UE.ajax.request(ajax_request_baseurl + '?get1=getcontent1&get2=getcontent2', {
  173. dataType: 'jsonp',
  174. onsuccess: function (r) {
  175. equal(r.get1, 'getcontent1', 'url上的参数1正常');
  176. equal(r.get2, 'getcontent2', 'url上的参数2正常');
  177. start();
  178. },
  179. onerror: function () {
  180. ok(false, 'fail to send jsonp request');
  181. start();
  182. }
  183. });
  184. stop();
  185. });
  186. test("jsonp请求,有数据,url上有数据", function () {
  187. UE.ajax.request(ajax_request_baseurl + '?get1=getcontent1&get2=getcontent2', {
  188. key1: 'keycontent1',
  189. key2: 'keycontent2',
  190. dataType: 'jsonp',
  191. onsuccess: function (r) {
  192. equal(r.get1, 'getcontent1', 'url上的参数1正常');
  193. equal(r.get2, 'getcontent2', 'url上的参数2正常');
  194. equal(r.key1, 'keycontent1', '数据上的参数1正常');
  195. equal(r.key2, 'keycontent2', '数据上的参数2正常');
  196. start();
  197. },
  198. onerror: function () {
  199. ok(false, 'fail to send jsonp request');
  200. start();
  201. }
  202. });
  203. stop();
  204. });
  205. test("jsonp请求,有数据,data上有数据,url上有数据", function () {
  206. UE.ajax.request(ajax_request_baseurl + '?get1=getcontent1&get2=getcontent2', {
  207. key1: 'keycontent1',
  208. key2: 'keycontent2',
  209. data: {
  210. 'datakey1': 'datakeycontent1',
  211. 'datakey2': 'datakeycontent2'
  212. },
  213. dataType: 'jsonp',
  214. onsuccess: function (r) {
  215. equal(r.get1, 'getcontent1', 'url上的参数1正常');
  216. equal(r.get2, 'getcontent2', 'url上的参数2正常');
  217. equal(r.key1, 'keycontent1', '数据上的参数1正常');
  218. equal(r.key2, 'keycontent2', '数据上的参数2正常');
  219. equal(r.datakey1, 'datakeycontent1', 'data数据上的参数1正常');
  220. equal(r.datakey2, 'datakeycontent2', 'data数据上的参数2正常');
  221. start();
  222. },
  223. onerror: function () {
  224. ok(false, 'fail to send jsonp request');
  225. start();
  226. }
  227. });
  228. stop();
  229. });
  230. test("通过getJSONP方法发送jsonp请求", function () {
  231. UE.ajax.getJSONP(ajax_request_baseurl + '?get1=getcontent1&get2=getcontent2', {
  232. 'datakey1': 'datakeycontent1',
  233. 'datakey2': 'datakeycontent2'
  234. }, function (r) {
  235. equal(r.get1, 'getcontent1', 'url上的参数1正常');
  236. equal(r.get2, 'getcontent2', 'url上的参数2正常');
  237. equal(r.datakey1, 'datakeycontent1', 'data数据上的参数1正常');
  238. equal(r.datakey2, 'datakeycontent2', 'data数据上的参数2正常');
  239. start();
  240. });
  241. stop();
  242. });