尝试从Chrome中的本地.html访问JSON API(Trying to access JSON API from local .html in Chrome)

编程入门 行业动态 更新时间:2024-10-05 11:17:47
尝试从Chrome中的本地.html访问JSON API(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。 关于什么可能出错的任何想法?

I have built an API and I m trying to access it from a test .html file in Chrome. I m accessing the file through file:///... and I get the local filesystem restrictions. I have enabled cross origin request access on my api but I get the following when I try to access it:

XMLHttpRequest cannot load http://. Method undefined is not allowed by Access-Control-Allow-Methods.

The request / response headers are as follows:

and the javascript code I m using is as follows:

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();

If I use $.getJSON from jquery the api call works fine but I dont want that. It needs to be pure javascript. Any ideas on what could be wrong?

最满意答案

对不起..我从来没有问过你是否需要它来测试又名调试或生产

使用此安全参数打开您的chrome:

C:\****\chrome.exe chromium-browser --disable-web-security

对于铬:

C:\****\chrome.exe google-chrome --disable-web-security

我正在使用铬但它也应该与铬配合使用..

Sorry.. I never asked if you need it for testing aka debugging or to production

open your chrome with this security param:

C:\****\chrome.exe chromium-browser --disable-web-security

for chrome:

C:\****\chrome.exe google-chrome --disable-web-security

I'm using chromium but it's should work with chrome too..

更多推荐

本文发布于:2023-08-07 11:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464080.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JSON   html   Chrome   local   access

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!