Forum Discussion

Lindsey's avatar
Lindsey
Leader
6 years ago

POST over HTTPS in jQuery

I have done some searching about how to post to the REST API over HTTPS in jQuery, and I still have a couple questions. I have attached my code below. I am trying to create a button that adds a subscription for a given label on every board.

  • How do I use the client ID? Do I just copy what's under the "CLIENT ID" column and pass that value in the header?
  • How do I get the user's authorization token for OAuth? I saw a post where someone tried to get the cookie called 'litok' - is that where it is stored?
  • With this code, I am getting an error saying I have been blocked by CORS policy. This code is inside an endpoint - is that why? How do I know if I already have CORS set up or not?

 

function updateSubscriptions(labelId, methodType) {
    const tenantId = <myTenantId>;
    const clientId = <myClientId>;
    const hostName = $(location).attr('hostname');
    let API_URL = 'api.lithium.com';
    if ((hostName.indexOf("stage") >= 0) || (hostName.indexOf("qa") >= 0)) {
        API_URL = 'api.stage.lithium.com';
    }
    $('.board-option').each( function () {
        const boardId = this.value;
        $.ajax({
            type: methodType,
            url: 'https://' + API_URL + '/community/2.0/' + tenantId + '/subscriptions',
            data: ({"data":{
                            "type":"subscription",
                            "target":{
                                "type":"label",
                                "id":labelId
                            },
                            "board": {
                                "id":boardId
                            }
                        }}),
            beforeSend: function(xhr) {
                xhr.setRequestHeader('authorization', 'Bearer ' + $.cookie('litok'));
                xhr.setRequestHeader('client_id', clientId);
                if (methodType === 'POST') {
                    xhr.setRequestHeader('content-type', 'application/json');
                }
            }
        });
    });
    const pageNumber = getCurrentPage();
    getEndpointAndDisplay(pageNumber);
}

 

 

 

 

 

 

No RepliesBe the first to reply