![](/img/trans.png)
[英]not able to fetch remote json link from local html file in chrome
[英]Trying to access JSON API from local .html in Chrome
我已經建立了一個API,並且試圖從Chrome中的測試.html文件訪問它。 我通過file:/// ...訪問文件,並且得到了本地文件系統限制。 我已經在我的api上啟用了跨源請求訪問,但是當我嘗試訪問它時得到以下信息:
XMLHttpRequest無法加載http://。 Access-Control-Allow-Methods不允許使用方法undefined。
請求/響應頭如下:
我正在使用的javascript代碼如下:
var xmlhttp;
if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest();
else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if (params.success) params.success(xmlhttp.responseText);
} else if (xmlhttp.readyState == 4) {
if (params.error) params.error(xmlhttp, xmlhttp.status, xmlhttp.responseText);
}
};
xmlhttp.open(params.method, params.url, true);
xmlhttp.setRequestHeader('Content-Type', 'application/json');
xmlhttp.setRequestHeader('Accept', 'application/json, text/javascript, */*; q=0.01');
if (params.data) xmlhttp.send(params.data);
else xmlhttp.send();
如果我從jquery使用$ .getJSON,則api調用可以正常運行,但我不希望這樣。 它必須是純JavaScript。 有什么想法可能有問題嗎?
對不起..我從沒問過您是否需要它來進行調試或調試?
使用此安全性參數打開鑲邊:
C:\\****\\chrome.exe chromium-browser --disable-web-security
對於鉻:
C:\\****\\chrome.exe google-chrome --disable-web-security
我正在使用鉻,但它也應該與鉻一起使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.