可以通过浏览器访问远程json组件,但不能通过$.getJSON()编程方式访问

时间:2022-02-28 22:46:59

I've been battling for 3 days trying to solve this and have 'google overload' - would love some help please.

我已经为解决这个问题奋斗了3天,并且“谷歌超负荷”——希望能得到一些帮助。

We have a Jenkins build server located on http://jenkinsBuild.mycompany.com:8080 so if I enter this url into a browser...

我们有一个Jenkins构建服务器位于http://jenkinsBuild.mycompany.com:8080,所以如果我将这个url输入到浏览器中……

http://jenkinsBuild.mycompany.com:8080/view/my_view/job/build_me/123/api/json?tree=result

http://jenkinsBuild.mycompany.com:8080 /视图/ my_view /工作/ build_me / 123 / api / json ?树=结果

... the browser page returns displaying...

…浏览器页面返回显示…

{"result":"SUCCESS"}

{“结果”:“成功”}

Now, according to the Jenkins Wiki, "Jenkins provides machine-consumable remote access API to its functionalities" supporting json and jsonp through a REST API, which I believe should circumvent any same origin policy issues.

现在,根据Jenkins Wiki的说法,“Jenkins为它的功能提供机器可消费的远程访问API”,通过REST API支持json和jsonp,我认为这应该可以避免任何相同的源策略问题。

I am attempting (using the latest Chrome browser) to get that same json component {"result":"SUCCESS"}.

我正在尝试(使用最新的Chrome浏览器)获得相同的json组件{"result":"SUCCESS"}。

I am using HTML/javascript with a $.getJSON() call as described below. The HTML file currently resides on my local machine, but will probably eventually live on a wiki. Console outputs for the three urls are listed after the code.

我使用的是HTML/javascript和$. getjson()调用,如下所述。HTML文件目前驻留在我的本地机器上,但可能最终将驻留在wiki上。三个url的控制台输出在代码之后列出。

How do I get the same json result I get by entering the url directly into the browser? Thanks for your help.

如何通过直接将url输入浏览器获得相同的json结果?谢谢你的帮助。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
    var url1 = "http://jenkinsBuild.mycompany.com:8080/view/my_view/job/build_me/123/api/json?tree=result";
    var url2 = "http://jenkinsBuild.mycompany.com:8080/view/my_view/job/build_me/123/api/json?tree=result&callback=?";
    var url3 = "http://jenkinsBuild.mycompany.com:8080/view/my_view/job/build_me/123/api/json?callback=?&tree=result";

    $('button').click(function(){
        $.getJSON(url1, function(json) {
            $("#reply").append("got callback: " + json);    
        });
    });
});
</script>

</head>
<body>

<button>Get Jenkins</button><br />

<div id="reply">

</div>

</body></html>

CONSOLE OUTPUT FOR THE THREE URL's...

三个URL的控制台输出…

url1 -> XMLHttpRequest cannot load http://jenkinsBuild.mycompany.com:8080/view/my_view/job/build_me/123/api/json?tree=result. Origin null is not allowed by Access-Control-Allow-Origin.

url1 -> XMLHttpRequest无法加载http://jenkinsBuild.mycompany.com:8080/view/my_view/job/build_me/123/api/json?tree=result。访问控制允许的起源不允许原点为空。

url2 -> Uncaught SyntaxError: Unexpected token : json:1

url2 ->未捕获的SyntaxError:意外令牌:json:1。

url3 -> Uncaught SyntaxError: Unexpected token : json:1

未捕获的SyntaxError:意外令牌:json:1

2 个解决方案

#1


1  

Updated:

更新:

Url1 is not a valid jsonp call because it's not specifying a callback. Not sure why url2 and url3 are failing.

Url1不是一个有效的jsonp调用,因为它没有指定回调。不知道url2和url3为什么会失败。

Here's another way try manually specify the callback function name:

另一种方法是手动指定回调函数名:

var url2 = "http://jenkinsBuild.mycompany.com:8080/view/my_view/job/build_me/123/api/json?tree=result&callback=my_local_javascript_function";

where my_local_javascript_function is a function in the javascript code on the calling browser. What will happen is that the server will respond with a script that looks something like this:

其中my_local_javascript_function是调用浏览器的javascript代码中的一个函数。会发生的情况是,服务器将使用如下脚本进行响应:

my_local_javascript_function({ //json object in here });

That function needs to be available on your local browser, and then it will be run. Please see here for more info on JSONP: http://en.wikipedia.org/wiki/JSONP

该函数需要在本地浏览器上可用,然后运行。有关JSONP的更多信息,请参见这里:http://en.wikipedia.org/wiki/JSONP

#2


0  

ajax calls follow the same origin policy, so both documents need to be on the same domain - not your case as the page that makes the call is on your local machine.

ajax调用遵循同源策略,所以两个文档都需要在同一个域上——不是您的情况,而是在本地机器上调用的页面。

#1


1  

Updated:

更新:

Url1 is not a valid jsonp call because it's not specifying a callback. Not sure why url2 and url3 are failing.

Url1不是一个有效的jsonp调用,因为它没有指定回调。不知道url2和url3为什么会失败。

Here's another way try manually specify the callback function name:

另一种方法是手动指定回调函数名:

var url2 = "http://jenkinsBuild.mycompany.com:8080/view/my_view/job/build_me/123/api/json?tree=result&callback=my_local_javascript_function";

where my_local_javascript_function is a function in the javascript code on the calling browser. What will happen is that the server will respond with a script that looks something like this:

其中my_local_javascript_function是调用浏览器的javascript代码中的一个函数。会发生的情况是,服务器将使用如下脚本进行响应:

my_local_javascript_function({ //json object in here });

That function needs to be available on your local browser, and then it will be run. Please see here for more info on JSONP: http://en.wikipedia.org/wiki/JSONP

该函数需要在本地浏览器上可用,然后运行。有关JSONP的更多信息,请参见这里:http://en.wikipedia.org/wiki/JSONP

#2


0  

ajax calls follow the same origin policy, so both documents need to be on the same domain - not your case as the page that makes the call is on your local machine.

ajax调用遵循同源策略,所以两个文档都需要在同一个域上——不是您的情况,而是在本地机器上调用的页面。