我已经构建了一个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..
更多推荐
发布评论