如何使用没有后端的注册表单向API发送POST请求

时间:2017-11-16 04:35:13

标签: javascript jquery html json api

我正在尝试弄清楚如何将我的注册连接到sender.net API,因此当有人填写表单时,它会被添加到我的订阅者列表中。 Sender.net向我提供了这些信息以帮助:

  1. API通过HTTP protocool使用POST实现。回复将在

  2. 中返回
  3. 请求的所有API均应指向http://app.sneder.net/api/api

  4. 请求数据必须在 POST参数命名数据中进行编码。

  5. 我写了这段代码,但我的订阅者列表中没有任何内容,请参阅下文:

    <!DOCTYPE html>
    <html>
    
    <head>
      <title>test with sender</title>
    </head>
    
    <body>
    
      <h1>This is to test sender.net API "POST" </h1>
    
      <form>
        <input type="text" name="Fname" placeholder="first name">
        <input type="text" name="Lname" placeholder="last name">
        <input type="email" name="Email" placeholder="email address">
        <button type="submit" onclick="JSONTest()"> Let the Post req begin! 
                </button>
      </form>
    
      <script type="text/javascript">
        JSONTest = function() {
          $.ajax({
            url: "https://app.sender.net/api/",
            method: "POST",
            datatype: "JSON",
            data: {
              method: "listSubscribe",
              params: {
                api_key: "my key number",
                list_id: "my id number",
                emails: ["Email", "Fname", "Lname"],
                update_existing: true
              }
            }
          });
        };
      </script>
    
      <!-- Jquery -->
      <script src="vendor/jquery/jquery.min.js"></script>
    
    </body>
    
    </html>
    

1 个答案:

答案 0 :(得分:0)

    

<head>
    <title>test with sender</title>
</head>

<body>

    <h1>This is to test sender.net API "POST" </h1>

    <form>
        <input type="text" name="Fname" placeholder="first name">
        <input type="text" name="Lname" placeholder="last name">
        <input type="email" name="Email" placeholder="email address">
        <button type="button" onclick="JSONTest()"> Let the Post req begin! 
        </button>
    </form>

    <script type="text/javascript">
    JSONTest = function() {
        $.ajax({
            url: "https://app.sender.net/api/",
            method: "POST",
            datatype: "JSON",
            data: {
                method: "listSubscribe",
                params: {
                    api_key: "my key number",
                    list_id: "my id number",
                    emails: ["Email", "Fname", "Lname"],
                    update_existing: true
                }
            },
            success : function(response) {
                console.log(response); // Feel free to Use debugger 

            }
        });
    };
    </script>