2

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

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

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

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

I am attempting (using the latest Chrome browser) to get that same json component {"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.

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

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

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.

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

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

Community
  • 1
  • 1
Drew
  • 477
  • 1
  • 5
  • 16
  • You probably have a same-origin issue. If the URL of the accessing page has a different domain or port number from the Jenkins server, the browser will not allow it. – fred02138 Oct 05 '13 at 15:21
  • possible duplicate of [Origin null is not allowed by Access-Control-Allow-Origin](http://stackoverflow.com/questions/8456538/origin-null-is-not-allowed-by-access-control-allow-origin) – Quentin Oct 05 '13 at 15:51
  • Thanks for your comments and answers all, but I'm pretty sure it is possible to do what I'm trying to do here, as the Jenkins REST API enables JSONP to get around the same-origin policy. There was a detailed second answer (with comments below) regarding how I should specify a callback function in my script and the URL I call the $.getJSON on - has somebody deleted that? I'd like to keep in here please, as it suggests something different to the possible duplicated linked above - thanks. – Drew Oct 05 '13 at 16:50
  • I correct my answer as per user2736012's comments and undeleted. – manishie Oct 05 '13 at 18:15
  • You need to use your developer console to view the request/response and see the content of the response. Because it's a SyntaxError when making the JSONP requests, it seems highly likely that the response being sent isn't formatted properly as JSONP. – user2736012 Oct 05 '13 at 18:17
  • possible duplicate of [Ways to circumvent the same-origin policy](http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy) – Bergi Oct 05 '13 at 18:45
  • @user2736012 I suspect you are right. I can't for the life of me find the request/response messages in the inspector console. But the comment at the bottom of `http://developer-blog.cloudbees.com/2013/05/taming-jenkins-json-api-with-depth-and.html` tends to indicate Jenkins doesn't support jsonp any longer (although I can't find any such official word supporting this notion). Without access to the remote domain I guess I am dead in the water. Thanks. – Drew Oct 07 '13 at 05:47

2 Answers2

1

Updated:

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

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({ //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

manishie
  • 5,302
  • 1
  • 20
  • 21
  • Why would the question mark make it fail? Seems more likely that OP is returning JSON formatting instead of JSONP formatting. – user2736012 Oct 05 '13 at 15:27
  • A question mark indicates the start of a query string. He's got two question marks now. '?tree=result&callback=?' which won't work. – manishie Oct 05 '13 at 15:30
  • 1
    It'll work, and it certainly wouldn't cause a SyntaxError either way. Anyway, jQuery uses this as an indicator of JSONP, and replaces the `?` with a function name. – user2736012 Oct 05 '13 at 15:33
  • @user2736012 - You are correct. I have updated my answer to remove the incorrect suggestions and simply offer an alternate way of manually naming the callback. – manishie Oct 05 '13 at 18:14
  • thanks for your suggestion @manishie - I've given you the tick for trying although it hasn't solved the issue - I suspect Jenkins doesn't do jsonp any more? Its annoying I can get access all the information I need about our Jenkins builds by entering a few dozen url's in browers and manually slicing and dicing the returned json info to say what builds have run/passed/failed etc, but I can't do that programmatically in javascript. An interesting exercise but a waste of a long weekend! Thanks for your help. – Drew Oct 07 '13 at 05:54
  • Wow, sorry you wasted the whole weekend! Been there. It sucks... Good luck! – manishie Oct 07 '13 at 05:55
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.

Christophe
  • 27,383
  • 28
  • 97
  • 140