自动调用REST API

时间:2019-08-19 11:14:20

标签: jquery ajax rest api

我想使用REST API自动发出请求。目的是通过单击提交按钮将数据自动提交到外部数据库。 我有下面的代码:

    <script type="text/javascript">

function callApi(){
  var cbIntegrationId = "xxxxxxx"; //Required
  var clientId = "xxxxxx"; //Required
  var clientSecret = "xxxxx"; //Required
  var tableName = "xxxx"; //Required
  var name = "My_Name";

    //Get access token
    $.post(
      "https://" + cbIntegrationId + ".caspio.com/oauth/token",
      {
        grant_type: "client_credentials",
        client_id: clientId,
        client_secret: clientSecret
      },
      function(cbAuth){
        //Run POST call
        $.ajax({
          url: "https://" + cbIntegrationId + ".caspio.com/rest/v2/tables/" + tableName + "/records?response=rows",
          type: 'POST',
          'data': JSON.stringify({ "Broker_Code": "Afinidade1004","UniqueID":"988","Drivers_Full_Name":"nome"}), //Define record values
          headers: {
            "Authorization": "Bearer " + cbAuth.access_token, //Extracts the access token from the initial authorization call
            "Content-Type": "application/json", //Required, otherwise 415 error is returned
            "Accept": "application/json"
          },
          dataType: 'json',
          success: function (data) {
            console.log(data.Result); //Check the console to view the new added row
          },
          error: function(data) {
            console.log(data.responseJSON); //Check the console to view error message if any
          }
        });
      }
    );
    }
    </script>

这是表格,但是不起作用。拜托,我需要帮助。

  <form onsubmit="callApi()">
Full Name: 
<input type="text" name="full_name" id="full_name"><br>
ID:
<input type="text" name="uniqueID" id="uniqueID"><br>
<input type="submit" value="Send">

0 个答案:

没有答案