Skip to content Skip to sidebar Skip to footer

Post Javascript Response To Caspio Table

i am trying to Post the player ID from onesignal to a caspio table. I have read dozens of forums etc and cant seem to get it right. Any Ideas? Custom is the Existing parameter for

Solution 1:

Before doing a POST call to Caspio API, you must generate an access token. I will share an AJAX solution which may represent security issues due to you are exposing your secret and client keys, also every call would generate a new token but it could be useful for any non tech user looking for an easy solution.

If you want a more robust solution, I could recommend the following service: Caspio API Library

At this point in time, v2 of Caspio API has been released so I am using this latest version of Caspio Rest API. The steps to run a POST call to Caspio service are these:

  1. Include jQuery in your web page head section or in your caspio datapage header. Do not include it in both as it may cause conflicts and it could increase the loading time of your page. Help to include jQuery in your project
  2. Generate an access token from Caspio API. Caspio Documentation / $.post() jQuery Documentation
  3. Run the POST call. Full Caspio API documentation

Complete code is shown bellow

<script>
var cbIntegrationId = "<your-integration-id>"; //Requiredvar clientId = "<your-client-id>"; //Requiredvar clientSecret = "<your-client-secret>"; //Requiredvar tableName = "<table-name>"; //Required//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: '{"<field1>": "<value-to-be-inserted>", "<field2>": "<value-to-be-inserted>"}', //Define record valuesheaders: {
        "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>

I strongly recommend taking a look at this article. Please read about Permissions for your API profile(s)

Post a Comment for "Post Javascript Response To Caspio Table"