network.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Dev: Network</title>
  7. <link href="../example/lib/weui.min.css" rel="stylesheet"/>
  8. <link href="../example/lib/demo.css" rel="stylesheet"/>
  9. <script src="../dist/vconsole.min.js"></script>
  10. </head>
  11. <body ontouchstart>
  12. <div class="page">
  13. <a onclick="getAjax()" href="javascript:;" class="weui_btn weui_btn_default">getAjax</a>
  14. <a onclick="postAjax()" href="javascript:;" class="weui_btn weui_btn_default">postAjax</a>
  15. <a onclick="getFetch()" href="javascript:;" class="weui_btn weui_btn_default">getFetch</a>
  16. <a onclick="getFetchSimple()" href="javascript:;" class="weui_btn weui_btn_default">getFetch(simple)</a>
  17. <a onclick="postFetch()" href="javascript:;" class="weui_btn weui_btn_default">postFetch</a>
  18. <a onclick="postFetchByRequest()" href="javascript:;" class="weui_btn weui_btn_default">postFetchByRequest</a>
  19. </div>
  20. </body>
  21. </html>
  22. <script>
  23. window.vConsole = new window.VConsole();
  24. function postAjax() {
  25. console.info('postAjax() Start, response should be logged after End');
  26. const xhr = new XMLHttpRequest();
  27. xhr.open('POST', './data/success.json');
  28. xhr.send({
  29. foo: 'bar',
  30. id: Math.random(),
  31. type: 'xhr',
  32. '<xss0>': '<xss1> XSS Attack!'
  33. });
  34. xhr.onload = () => {
  35. console.log('postAjax Response:', JSON.parse(xhr.response));
  36. };
  37. xhr.onerror = () => {
  38. console.log('postAjax Error');
  39. };
  40. console.info('postAjax() End');
  41. }
  42. function getAjax() {
  43. console.info('getAjax() Start, response should be logged after End');
  44. const url = './data/success.json?type=xhr&id=' + Math.random() + '&<xss0>';
  45. const xhr = new XMLHttpRequest();
  46. xhr.open('GET', url);
  47. xhr.setRequestHeader('custom-header', 'foobar');
  48. xhr.send();
  49. xhr.onload = () => {
  50. console.log('getAjax Response:', JSON.parse(xhr.response));
  51. };
  52. xhr.onerror = () => {
  53. console.log('getAjax Error');
  54. };
  55. console.info('getAjax() End');
  56. }
  57. function getFetch() {
  58. console.info('getFetch() Start, response should be logged after End');
  59. window.fetch('./data/success.json?type=fetch&id=' + Math.random(), {
  60. method: 'GET',
  61. headers: {
  62. 'custom-header': 'foobar',
  63. 'content-type': 'application/json'
  64. },
  65. }).then((data) => {
  66. return data.json();
  67. }).then((data) => {
  68. console.log('getFetch Response:', data);
  69. });
  70. console.info('getFetch() End');
  71. }
  72. function getFetchSimple() {
  73. console.info('getFetchSimple() Start, response should be logged after End');
  74. window.fetch('./data/success.json?type=fetch&id=' + Math.random()).then((data) => {
  75. return data.json();
  76. }).then((data) => {
  77. console.log('getFetchSimple Response:', data);
  78. });
  79. console.info('getFetchSimple() End');
  80. }
  81. function postFetch() {
  82. console.info('postFetch() Start, response should be logged after End');
  83. window.fetch('./data/success.json', {
  84. method: 'POST',
  85. body: { foo: 'bar', id: Math.random(), type: 'fetch' },
  86. // body: new Blob([new ArrayBuffer(233)], { type: 'image/png' }),
  87. headers: {
  88. 'custom-header': 'foobar',
  89. // 'content-type': 'application/json'
  90. // 'content-type': 'application/x-www-form-urlencoded'
  91. 'content-type': 'multipart/form-data',
  92. 'xss': '<xss1> XSS Attack!'
  93. },
  94. }).then((data) => {
  95. return data.json();
  96. }).then((data) => {
  97. console.log('postFetch Response:', data);
  98. }).catch((error) => {
  99. console.error('postFetch Error:', error);
  100. });
  101. console.info('postFetch() End');
  102. }
  103. function postFetchByRequest() {
  104. console.info('postFetchByRequest() Start, response should be logged after End');
  105. const headers = new Headers();
  106. headers.append('custom-header', 'foobar');
  107. // headers.append('content-type', 'application/json');
  108. headers.append('content-type', 'application/x-www-form-urlencoded');
  109. const req = new Request('./data/success.json?type=fetch&id=' + Math.random(), {
  110. method: 'POST',
  111. body: { foo: 'bar', id: Math.random() },
  112. // body: new Blob([new ArrayBuffer(233)], { type: 'image/png' }),
  113. headers: headers,
  114. });
  115. window.fetch(req).then((data) => {
  116. return data.json();
  117. }).then((data) => {
  118. console.log('postFetchByRequest Response:', data);
  119. }).catch((error) => {
  120. console.error('postFetchByRequest Error:', error);
  121. });
  122. console.info('postFetchByRequest() End');
  123. }
  124. </script>