简体   繁体   English

使用服务器端Ruby REST API的Java Ajax Client应用程序中的错误

[英]Error in Java Ajax Client application consuming a server side Ruby REST API

I have a client side application in java (JSP + AJAX + JQUERY) and a server side application in ruby that exposes a REST API. 我在Java中有一个客户端应用程序(JSP + AJAX + JQUERY),在Ruby中有一个服务器端应用程序,它公开了REST API。 i am trying to have the client application consume this ruby based REST API using the following code: 我正在尝试使用以下代码让客户端应用程序使用基于ruby的REST API:

$.ajax({
    type: 'GET',
    url: 'http://localhost:4567/apps',
    success : function(data){
        alert ('success'); 
    },
    error : function(jqXHR, textStatus, errorThrown){
        alert(jqXHR.status);
        alert(textStatus);
        alert(errorThrown);
    }
}); 

However, the error component is invoked each time I try. 但是,每次尝试时都会调用错误组件。 The server side code is as follows: 服务器端代码如下:

get "/apps" do
 apps = get_apps
 apps.map { |a| a.name }.to_json
end

When I make a call to the above REST API using browser 当我使用浏览器调用上述REST API时

(http://localhost:4567/apps) 

I get the expected response which is a string of app names in json format. 我得到预期的响应,它是json格式的应用程序名称字符串。

Also, I checked the ruby console, for calls from browser I have the following response: 另外,我检查了ruby控制台,对于来自浏览器的调用,我有以下响应:

Computer Name - - [15/Apr/2014:12:29:19 India Standard Time] "GET /apps HTTP/1.1" 200 186

and for calls from the java application all I have on the console is: 对于来自Java应用程序的调用,我在控制台上所拥有的就是:

http://localhost:8080/MyApp/pages/index.jsp -> /apps

I wonder what is wrong here. 我不知道这是怎么了。 Would be great if some one could help out please. 如果有人可以帮忙,那会很好。

Found the answer to my own question. 找到了我自己问题的答案。

Same-origin policy restricts pages running scripting to access DOM on different sites. 同源策略限制运行脚本的页面访问不同站点上的DOM。 Since this policy applies to XMLHttpRequest, I was not able to get through. 由于此策略适用于XMLHttpRequest,因此无法通过。

Please see http://en.wikipedia.org/wiki/Same-origin_policy for further details. 有关更多详细信息,请参见http://en.wikipedia.org/wiki/Same-origin_policy

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

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