0

i need to fetch json data from cloudify rest url (remotehost) http://hostname:8100/service/applications/.

This URL returns the following JSON response:

{
    "response": {
        "petclinic": ""
    },
    "status": "success"
}

I've tried to get the response with the code below:

$.getJSON("http://hostname:8100/service/applications?jsoncallback=?", function (result) {
    $.each(result, function (i, field) {
        $("div").append(field + " ");
    });
});

I checked request from net panel in firebug, which is showing a 200 OK status, but I can't get the JSON data. The error bellow appears in the console:

console error

Eliran Malka
  • 15,821
  • 6
  • 77
  • 100
Jaydipsinh
  • 491
  • 1
  • 9
  • 27
  • 2
    what happens if you add an `error` or `complete` handler? – cfs Jul 23 '13 at 12:03
  • i tried this $.getJSON( "http://ip:8100/service/applications") .done(function( json ) { console.log( "JSON Data: " + json); }) .fail(function( jqxhr, textStatus, error ) { var err = textStatus + ', ' + error; console.log( "Request Failed: " + err); }); in console output i got bellow error Request Failed: error, test.html (line 14) – Jaydipsinh Jul 23 '13 at 12:09
  • So what happened when you tried that? – cfs Jul 23 '13 at 12:10
  • can you give simple example ? – Jaydipsinh Jul 23 '13 at 12:11
  • I didn't see the edit to your comment... So it looks like you have an error on line 14 of test.html. That's why your success handler wasn't being called. – cfs Jul 23 '13 at 12:18
  • actually i'm trying to run test.html file by dbl clicking on that.. i m not using any server. so is it oky? – Jaydipsinh Jul 23 '13 at 12:30
  • I'm not sure what you mean by "run test.html file by dbl clicking on that", but you have a javascript error on line 14 of that file, so I would start debugging that and try posting another question to help resolve that specific issue – cfs Jul 23 '13 at 12:51
  • i solved that error. but tell me onething to run $.ajax function we need to run that file from server or not ? i mean i dbl click on html file which contain $.ajax function so that code will run or not? – Jaydipsinh Jul 23 '13 at 12:56
  • You need to open an html file in your browser, when you do that any javascript will be executed (assuming you haven't disable javascript in your browser). It won't matter whether the file is hosted locally or remotely (on a server) unless you have server-side scripting (like php) that needs to be run. – cfs Jul 23 '13 at 13:02
  • oky thanks, even i put that file on localhost and try to run that file on localhost but still i can't get json data. in firebug console i found GET http://ip:8100/service/applications 200 OK 309ms in red color. – Jaydipsinh Jul 23 '13 at 13:17
  • i tried with another datatype called JSONP but there i got error in console says : invalid label (point to json data) – Jaydipsinh Jul 23 '13 at 13:43
  • The best way to get help is to post a new question explaining what you're attempting to do, the code you are using to do it, and any error messages you are seeing. Writing about new problems you are encountering in the comments won't get much attention from the community and won't be very helpful to future users with a similar problem. Good luck! – cfs Jul 23 '13 at 13:52

1 Answers1

2

Same Origin

Assuming the call is made from the same domain (e.g. you're deploying a local cloud, and your call is made locally, and sends a request to localhost:8100), you should be able to succeed performing REST calls, provided that you remove the ?jsoncallback=? from the request URL.

This suffix to the URL serves to request a wrapper function on the response, and is used with JSONP (JavaScript-Object-Notation Padding) type requests. In order to use JSONP the server must support it - which currently the Cloudify REST API does not - thus such requests will fail, from any origin.

Cross Origin

If you're trying to run this from a different domain than that of the REST server, it won't work regardless. The REST API does not allow for Cross Origin Resource Sharing either. It is meant for use by servers, so create a service that makes the calls to the REST API and serve your client with that. You can than put your jQuery XHR calls on that client, calling your own service.

Further reading

Eliran Malka
  • 15,821
  • 6
  • 77
  • 100
  • i also tried without jsoncallback but still i'm not getting json data. – Jaydipsinh Jul 24 '13 at 10:39
  • did you try a simple test case? e.g. `$.getJSON("http://hostname:8100/service/applications",function(result) { alert(result); });`. anyhow you shouldn't accept the answer if it doesn't solve the issue at hand. – Eliran Malka Jul 24 '13 at 11:17
  • oh, and, i assume the request is running from a client on the same domain (i.e. `http://hostname/` in our case). is it? – Eliran Malka Jul 24 '13 at 11:23
  • i tried with your given code but still alert is not coming. and i check net panel in firebug so there i found that request and even response 200 ok is der but alert box is not appear still. and actually i bootstrap ec2 cloud and there on ec2 node i install 2 application. so i'm using rest url of ec2. and i m executing this jquery code from localhost – Jaydipsinh Jul 24 '13 at 13:41