尝试在Chrome中从本地.html访问JSON API [英] Trying to access JSON API from local .html in Chrome

查看:546
本文介绍了尝试在Chrome中从本地.html访问JSON API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个API,并尝试从Chrome中的测试.html档案存取。我通过file:/// ...访问文件,我得到本地文件系统限制。我已经启用跨我起点请求访问我的API,但我得到以下当我尝试访问它:


XMLHttpRequest无法加载http: /。方法未定义
不允许由Access-Control-Allow-Methods。


请求/响应标题如下:





并且使用的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();

如果我使用$ .getJSON从jquery api调用工作正常,但我不想那样。它需要是纯javascript。对于什么可能是错误的任何想法?

解决方案

抱歉..我从来没有问过,或生产



使用此安全参数打开Chrome:



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



/ p>

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



我使用铬,但它也应该与铬一起使用。


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?

解决方案

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..

这篇关于尝试在Chrome中从本地.html访问JSON API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆