在Apache localhost中使用Ajax发布到https API

时间:2019-06-17 07:52:23

标签: javascript ajax api

我正在尝试创建一个带有引导程序的登录页面,单击该按钮后,将通过“ 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>

我没有收到任何OTP

我已经在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。

1 个答案:

答案 0 :(得分:0)

下面的代码是否可能格式错误

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

缺少逗号

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

此外,您还两次对arr进行了字符串化。您是要发送这条线

 let json = JSON.stringify(arr);