2016-11-04 102 views
1

我有一個網站,我想使用pushpad。一切正常,但我想顯示訂閱的數量。該網站說我必須使用REST api,但我以前從未使用過ajax。爲什麼這個pushpad腳本不能工作

$.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"); 
     } 
    }); 

該代碼給出了一個錯誤:

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

如果我改變數據類型爲JSON p我得到另一個錯誤:

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

我搜索了一段時間,但我沒有」 t找到任何相關的問題,所以我希望你能幫助我。

回答

0

你不能使用Javascript(你得到的錯誤與CORS有關)。

您必須使用您的服務器端語言(PHP,Ruby,Node.js等)檢索訂閱者數量。

此外,如果您只需要訂閱數量,我建議您使用GET /projects/PROJECT_ID,然後在響應中閱讀subscriptions_count字段。

相關問題