简体   繁体   English

如何使用jquery从cloudify rest api读取json响应?

[英]how to read json response from cloudify rest api using jquery?

i need to fetch json data from cloudify rest url (remotehost) http://hostname:8100/service/applications/ . 我需要从cloudify rest url(remotehost) http://hostname:8100/service/applications/获取json数据。

This URL returns the following JSON response: 此URL返回以下JSON响应:

{
    "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. 我检查了来自firebug的网络面板的请求,显示200 OK状态,但我无法获取JSON数据。 The error bellow appears in the console: 控制台中出现以下错误:

控制台错误

Same Origin 同源

Assuming the call is made from the same domain (eg 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=? 假设呼叫来自同一个域 (例如,您正在部署本地云 ,并且您的呼叫是在本地进行的,并向localhost:8100发送请求localhost:8100 ),您应该能够成功执行REST呼叫,前提是您已删除?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. URL的后缀用于请求响应的包装函数,并与JSONP(JavaScript-Object-Notation Padding)类型请求一起使用。 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. 为了使用JSONP,服务器必须支持它 - 当前Cloudify REST API不支持它 - 因此这些请求将从任何来源失败。

Cross 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. 如果您尝试从与REST服务器不同的域运行此 ,则无论如何都无法运行。 The REST API does not allow for Cross Origin Resource Sharing either. REST API也不允许跨源资源共享。 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. 它适用于服务器,因此创建一个服务,该服务调用REST API并为其提供服务。 You can than put your jQuery XHR calls on that client, calling your own service. 您可以将jQuery XHR调用放在该客户端上,调用您自己的服务。

Further reading 进一步阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM