2017-04-12 63 views
0

我在寫一個擴展程序,當特定遊戲收到更新時應該通知用戶。數據從MySQL數據庫接收。在HTTPS上阻止Ajax呼叫 - Chrome擴展程序

我做了一個ajax調用來接收由我的本地主機服務器上的PHP腳本提供的json數據(用於測試目的)。

那麼,它在HTTP網站上正常工作。但是腳本被HTTPS網站阻止。

該消息「此網頁正試圖加載來自未經驗證的來源的腳本」,從而導致此故障排除頁從谷歌: Link

有什麼建議?

的清單:

{ 
"manifest_version": 2, 
"name": "Extension", 
"version": "1.0", 
"icons": { 
    "128": "icon128.png", 
    "48": "icon48.png", 
    "16": "icon16.png" 
}, 
"page_action": { 
    "default_icon": "icon16.png", 
    "default_popup": "popup.html" 
}, 
"background":{ 
    "scripts" : ["eventPage.js"], 
    "persistent": false 
}, 
"content_scripts": [ 
    { 
     "matches": ["<all_urls>"], 
     "js": ["content.js", "jquery-3.2.0.min.js"], 
     "css": ["content.css"], 
     "run_at": "document_start", 
     "match_about_blank": true 
    } 
], 
"permissions": [ 
    "notifications", "http://localhost/*", "tabs" 
]} 

eventPage.js

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { 

chrome.tabs.query({active: true, currentWindow: true}, function(tabs){ 
    chrome.tabs.sendMessage(tabs[0].id, {todo: "displayNews"}); 
}); 
}); 

content.js

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){ 
if (request.todo == "displayNews"){ 
    $.ajax({ 
     url: 'http://localhost/database/connect.php', 
     type:'POST', 
     dataType: 'json', 
     success: function(output_string){ 
       alert(output_string[1].url); 
      }, 
       error: function (xhr, ajaxOptions, thrownError){ 
       alert(xhr.statusText); 
       alert(thrownError); 
       } 
    }); 
//Some other code is executed here 
}}); 
+0

[您可能會發現這個網站很有幫助](https://developer.chrome.com/extensions/xhr) –

+0

HTTPS站點不允許對HTTP API進行AJAX調用。您需要在本地主機上實施HTTPS服務器。 – Barmar

回答