Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未获取返回有效JSON的Localhost API_Javascript_Json_Tomcat_Cors - Fatal编程技术网

Javascript 未获取返回有效JSON的Localhost API

Javascript 未获取返回有效JSON的Localhost API,javascript,json,tomcat,cors,Javascript,Json,Tomcat,Cors,我使用ApacheTomcat托管本地服务器,并使用jersey创建了JavaRESTAPI。现在我返回JSON对象,当我在浏览器或邮递员中打开它时,它工作正常,并且它也是有效的JSON对象 [ { "algo_name": "First Come First Served", "running_time": 13.125098 }, { "algo_name": "Shortest Job First", "running_time": 22.620548 },

我使用ApacheTomcat托管本地服务器,并使用jersey创建了JavaRESTAPI。现在我返回JSON对象,当我在浏览器或邮递员中打开它时,它工作正常,并且它也是有效的JSON对象

[
{
    "algo_name": "First Come First Served",
    "running_time": 13.125098
},
{
    "algo_name": "Shortest Job First",
    "running_time": 22.620548
},
{
    "algo_name": "Shortest Remaining Time",
    "running_time": 21.530843
},
{
    "algo_name": "Non-Preemptive Highest Priority First",
    "running_time": 15.412454
},
{
    "algo_name": "Round Robin",
    "running_time": 20.904071
}
]
但是,当我试图使用url“”从html文件中的javascipt读取它时,我无法从中获取json

let url = 'http://localhost:8080/algo/webapi/handler/100/100/100/4';

fetch( url, {
    method: 'get'
}).then( function( response ) {
    alert("Success");
}).catch( function( err ) {
    alert("OOps");
});
上面的代码在我的浏览器中给出了“OOps”。我无法从本地计算机上的javascript获得响应。下面是浏览器中的错误日志

Failed to load http://localhost:8080/algo/webapi/handler/100/100/100/4:
No 'Access-Control-Allow-Origin' header is present on the requested
resource.Origin 'null' is therefore not allowed access. If an opaque 
response serves your needs, set the request's mode to 'no-cors' to fetch the 
resource with CORS disabled.

我还尝试返回xml,但它也不起作用?问题是什么?

问题是我没有在JAVA Api中包含CORS响应库。虽然邮递员不用它也能回复。浏览器不会

您应该看看
err
。您是否尝试使用
cURL
或邮递员,或任何其他媒介获取JSON?是的,我使用邮递员。