【问题标题】:Using ajax post to a https API in apache localhost在 apache localhost 中使用 ajax post 到 https API
【发布时间】:2019-06-17 07:52:23
【问题描述】:

我正在尝试使用引导程序创建一个登录页面,在单击按钮时,将通过“POST”对 API 进行 ajax 调用,这应该将 OTP 发送到我的手机。

我在 apache localhost 上运行它。在检查源时,我收到错误

HTTP400: BAD REQUEST - The request could not be processed by the server due to invalid syntax.

(XHR)POST - https://<api>

我没有收到任何一次性密码

我已经在 Postman 上测试过 API 并且它们可以工作。我尝试使用语法,但没有任何效果。

    <!--body>
    <form class="login100-form validate-form p-l-55 p-r-55 p-t-178" action="/Login_v8/otp.html">
     <span class="login100-form-title">Sign In</span>
     <div class="wrap-input100 validate-input m-b-16" data-validate="Please enter phone number">
      <input class="input100" type="phone" name="Ph" placeholder="Phone number" id="Ph">
      <span class="focus-input100"></span>
     </div>
     <div class="text-right p-t-13 p-b-23">
      <span class="txt1">Forgot</span>
      <a href="#" class="txt2">Username / Password?</a>
     </div>
     <div class="container-login100-form-btn">
      <button class="login100-form-btn" onclick="generate_otp()">Sign in</button>
     </div>
     <!-- other lines of code>
    </form>
    <!--other lines of code>
    <script>
     function generate_otp(){
      var request = new XMLHttpRequest();
      var Ph = document.getElementById('Ph');
      var arr = {Ph: "968*******",
       device:{
        deviceId: "1234"
        platform: "1",
        appType: "0"
       }
      };
      let json = JSON.stringify(arr);
      request.open("POST","<api>", false);
      request.setRequestHeader('Content-type','application/json');
      request.send(JSON.stringify(arr));
     }
    </script>

它应该向我的手机发送 OTP。

【问题讨论】:

    标签: javascript ajax api


    【解决方案1】:

    下面的代码有可能是错误的

        var arr = {
        Ph: "968*******",
        device: {
            deviceId: "1234"
            platform: "1",
            appType: "0"
        }
    };
    

    少了一个逗号

            ....
            deviceId: "1234",
            platform: "1",
            .....
    

    另外,你已经对 arr 进行了两次字符串化。你的意思是把这条线送出去吗

     let json = JSON.stringify(arr);
    

    【讨论】:

      猜你喜欢
      • 2017-05-07
      • 2016-03-11
      • 1970-01-01
      • 1970-01-01
      • 2019-12-06
      • 1970-01-01
      • 1970-01-01
      • 2010-10-10
      • 1970-01-01
      相关资源
      最近更新 更多