本文介绍了对Caspio表的POST javascript响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将玩家ID从onesignal发布到caspio表.我已经阅读了数十个论坛等内容,但似乎做对了.有任何想法吗? 自定义"是我要与用户ID一起发送的此页面上用户名的现有参数.

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 username on this page that i want sent along with the user id.

 OneSignal.push(function() { 
OneSignal.on('subscriptionChange', function (isSubscribed) {

        if (isSubscribed) {
            OneSignal.getUserId(function(userId) { 
                $.post(https://xxxxxxxx.caspio.com/rest/v1/tables/User_ID_Test/rows,( User_ID = 'userId' User_Name = '[@custom]' ), function (data));
            });
        }
    });

推荐答案

在对Caspio API进行POST调用之前,必须生成访问令牌.我将分享一个AJAX解决方案,该解决方案可能会由于您公开您的秘密密钥和客户端密钥而引起安全问题,而且每次调用都会生成一个新令牌,但对于寻求简单解决方案的任何非技术用户来说都可能有用.

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.

如果您想要一个更强大的解决方案,我可以推荐以下服务 : Caspio API库

目前,Caspio API v2已发布,因此我正在使用此最新版本的Caspio Rest API.运行对Caspio服务的POST调用的步骤如下:

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. 在您的网页标题部分或caspio数据页标题中包含jQuery. 请勿同时将其包含在其中,因为这可能会导致冲突,并且可能会增加页面的加载时间 . 帮助在项目中包含jQuery
  2. 从Caspio API生成访问令牌.Caspio文档/$ .post()jQuery文档
  3. 运行POST呼叫. 完整的Caspio API文档
  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

完整的代码显示在下方

<script>
var cbIntegrationId = "<your-integration-id>"; //Required
var clientId = "<your-client-id>"; //Required
var clientSecret = "<your-client-secret>"; //Required
var 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 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>

我强烈建议您查看本文.请阅读有关您的API配置文件的权限

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

这篇关于对Caspio表的POST javascript响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 16:42