form.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var Ajax = {
  2. get: function (url, callback) {
  3. // XMLHttpRequest对象用于在后台与服务器交换数据
  4. var xhr = new XMLHttpRequest()
  5. xhr.open('GET', url, false)
  6. xhr.onreadystatechange = function () {
  7. // readyState == 4说明请求已完成
  8. if (xhr.readyState === 4) {
  9. if (xhr.status === 200 || xhr.status === 304) {
  10. console.log(xhr.responseText)
  11. callback(xhr.responseText)
  12. }
  13. }
  14. }
  15. xhr.send()
  16. },
  17. // data应为'a=a1&b=b1'这种字符串格式,在jq里如果data为对象会自动将对象转成这种字符串格式
  18. post: function (url, data, callback) {
  19. var xhr = new XMLHttpRequest()
  20. xhr.open('POST', url, false)
  21. // 添加http头,发送信息至服务器时内容编码类型
  22. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
  23. xhr.onreadystatechange = function () {
  24. if (xhr.readyState === 4) {
  25. if (xhr.status === 200 || xhr.status === 304) {
  26. // console.log(xhr.responseText);
  27. callback(xhr.responseText)
  28. }
  29. }
  30. }
  31. xhr.send(data)
  32. }
  33. }
  34. ;(() => {
  35. 'use strict'
  36. var apkUrl = ''
  37. $.get('/api/properties/apk', function (data) {
  38. apkUrl = data.value
  39. })
  40. // Fetch all the forms we want to apply custom Bootstrap validation styles to
  41. const forms = document.querySelectorAll('.needs-validation')
  42. // Loop over them and prevent submission
  43. Array.from(forms).forEach((form) => {
  44. form.addEventListener(
  45. 'submit',
  46. (event) => {
  47. console.log(form.checkValidity())
  48. // /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
  49. if (!form.checkValidity()) {
  50. event.preventDefault()
  51. event.stopPropagation()
  52. } else {
  53. event.preventDefault()
  54. event.stopPropagation()
  55. Ajax.post(
  56. '/api/referrer',
  57. 'email=' +
  58. document.getElementById('email').value +
  59. '&referrer=' +
  60. GetQueryString('referrer'),
  61. () => {}
  62. )
  63. window.location.href = apkUrl
  64. // /api/referrer
  65. }
  66. form.classList.add('was-validated')
  67. },
  68. false
  69. )
  70. })
  71. })()