jQuery 如何解决请求的资源上不存在“Access-Control-Allow-Origin”标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49883790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve no 'Access-Control-Allow-Origin' header is present on the requested resource
提问by Sudarsan San
CORS:A very common issue that most of the developers face when they hit a rest service from another domain and so do I.
CORS:大多数开发人员在遇到来自另一个域的休息服务时面临的一个非常常见的问题,我也是。
I get this error :
我收到此错误:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.
请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问Origin ' http://localhost:8080'。
Below is the jsp snippet.
下面是jsp片段。
<html>
<head>
<meta content="text/html; charset=utf-8">
<title>AJAX JSON SAMPLE</title>
<script type="application/javascript">
function load()
{
var url = "https://samplewebapp1.herokuapp.com/rest/json/get";//use any url that have json data
var request;
if(window.XMLHttpRequest){
request=new XMLHttpRequest();//for Chrome, mozilla etc
}
else if(window.ActiveXObject){
request=new ActiveXObject("Microsoft.XMLHTTP");//for IE only
}
request.onreadystatechange = function(){
if (request.readyState == 4 )
{
var jsonObj = JSON.parse(request.responseText);//JSON.parse() returns JSON object
document.getElementById("appName").innerHTML = jsonObj.appName;
document.getElementById("language").innerHTML = jsonObj.language;
}
}
request.open("GET", url, true);
request.send();
}
</script>
</head>
<body>
appName:
<span id="appName"></span>
<br /> language:
<span id="language"></span>
<br />
<button type="button" onclick="load()">Load Information</button>
</body>
</html>
Below is the rest service implementation of that service.
下面是该服务的其余服务实现。
`package com.heroku.rest;
import java.util.Date;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.heroku.model.Heroku;
@Path("/json")
public class HerokuRestService {
@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public Heroku getTrackInJSON() {
return new Heroku("My First Heroku", "Java", new Date().toString());
} }`
What am i missing.?
我错过了什么。?
采纳答案by Ramkishore V S
You are trying to do a XmlHttpRequest to some other domain. You could simply use CORS to tell your browser to allow it. (It's completely browser specific due to some security reasons).
您正在尝试对其他域执行 XmlHttpRequest。您可以简单地使用 CORS 告诉您的浏览器允许它。(由于某些安全原因,它完全特定于浏览器)。
https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en-USuse this extension to allow access to no 'access-control-allow-origin' header request.
https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en-US使用此扩展来允许访问无“access-control-allow-origin”标头请求。
Or, you could manually configure CORS as well. Like described here https://www.html5rocks.com/en/tutorials/cors/
或者,您也可以手动配置 CORS。就像这里描述的https://www.html5rocks.com/en/tutorials/cors/