jqueryajaxweb-pushpushpad

Why doesn't this pushpad script works


I have a website and I want to use pushpad. Everything works alright but I want to display the number of subscriptions. The site says I have to use the REST api, but I never used ajax before.

$.ajax({
       type:"GET",
       contentType: "application/json",
       beforeSend: function (request)
       {
         request.setRequestHeader("Accept", "application/json");
          request.setRequestHeader("Authorization", "Token token='mytoken'");
       },
      crossDomain: true,
      dataType: 'json',
       url: "https://pushpad.xyz/projects/projectid/subscriptions",
       success: function(msg) {
           alert("success");
       }
   });

This code gives an error:

No 'Access-Control-Allow-Origin' header is present on the requested resource.

If I change dataType to jsonp I get another error:

Refused to execute script from 'currectURL' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

I searched for a while but I didn't find any related question, so I hope you could help me.


Solution

  • You cannot use Javascript (the error you get is related to CORS).

    You must retrieve the number of subscribers using your server side language (PHP, Ruby, Node.js, etc.).

    Also, if you just need the number of subscriptions I suggest that you use GET /projects/PROJECT_ID and then you read the subscriptions_count field in the response.