jQuery ajax 调用 REST 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7112672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:25:23  来源:igfitidea点击:

jQuery ajax call to REST service

jqueryrest

提问by DaHymanal

I'm trying to make an ajax call from jquery to a rest service. The rest service used is right from a tutorial of mkyong's blog, this one: http://www.mkyong.com/webservices/jax-rs/integrate-Hymanson-with-resteasy/

我正在尝试从 jquery 向休息服务进行 ajax 调用。使用的休息服务来自 mkyong 的博客教程,这个:http://www.mkyong.com/webservices/jax-rs/integrate-Hymanson-with-resteasy/

The service works, but when i try to make a call from jQuery, in Firebug there is a 200 status code, but in the response section, nothing.

该服务有效,但是当我尝试从 jQuery 进行调用时,在 Firebug 中有一个 200 状态代码,但在响应部分,什么都没有。

Here is the html page with the ajax call:

这是带有 ajax 调用的 html 页面:

<html>
<head>
    <script type="text/javascript" src="jquery-1.6.2.min.js"></script>
</head>

<body>  

<button id="ajax">ajax call</button>
<button id="json">json</button>

<script type="text/javascript">
    $('#json').click(function(){ 
        alert('json');
         $.getJSON("http://localhost:8080/restws/json/product/get",
         function(data) {
            alert(data);         
          });   
    });

    $('#ajax').click(function(){ 
        alert('ajax');
         $.ajax({ 
             type: "GET",
             dataType: "json",
             url: "http://localhost:8080/restws/json/product/get",
             success: function(data){        
                alert(data);
             }
         });
    });

</script>



</body>

</html>

I can't figure it out where I went wrong, could you please tell me what i am doing wrong?

我不知道我哪里出错了,你能告诉我我做错了什么吗?

Thanks!

谢谢!

回答by Rocket Hazmat

You are running your HTML from a different host than the host you are requesting. Because of this, you are getting blocked by the same origin policy.

您正在从与您请求的主机不同的主机运行您的 HTML。因此,您会被同源策略阻止。

One way around this is to use JSONP. This allows cross-site requests.

解决此问题的一种方法是使用JSONP。这允许跨站点请求。

In JSON, you are returned:

在 JSON 中,您将返回:

{a: 5, b: 6}

In JSONP, the JSON is wrapped in a function call, so it becomes a script, and not an object.

在 JSONP 中,JSON 被封装在一个函数调用中,因此它变成了一个脚本,而不是一个对象。

callback({a: 5, b: 6})

You need to edit your REST service to accept a parameter called callback, and then to use the value of that parameter as the function name. You should also change the content-typeto application/javascript.

您需要编辑 REST 服务以接受名为 的参数callback,然后将该参数的值用作函数名称。您还应该将 更改content-typeapplication/javascript

For example: http://localhost:8080/restws/json/product/get?callback=processshould output:

例如:http://localhost:8080/restws/json/product/get?callback=process应该输出:

process({a: 5, b: 6})

In your JavaScript, you will need to tell jQuery to use JSONP. To do this, you need to append ?callback=?to the URL.

在您的 JavaScript 中,您需要告诉 jQuery 使用 JSONP。为此,您需要附加?callback=?到 URL。

$.getJSON("http://localhost:8080/restws/json/product/get?callback=?",
   function(data) {
     alert(data);         
   });

If you use $.ajax, it will auto append the ?callback=?if you tell it to use jsonp.

如果您使用$.ajax,它会自动附加 ,?callback=?如果您告诉它使用jsonp

$.ajax({ 
   type: "GET",
   dataType: "jsonp",
   url: "http://localhost:8080/restws/json/product/get",
   success: function(data){        
     alert(data);
   }
});

回答by DaHymanal

From the use of 8080 I'm assuming you are using a tomcat servlet container to serve your rest api. If this is the case you can also consider to have your webserver proxy the requests to the servlet container.

从使用 8080 开始,我假设您正在使用 tomcat servlet 容器来为您的其余 api 提供服务。如果是这种情况,您还可以考虑让您的网络服务器代理对 servlet 容器的请求。

With apache you would typically use mod_jk (although there are other alternatives) to serve the api trough the web server behind port 80 instead of 8080 which would solve the cross domain issue.

使用 apache,您通常会使用 mod_jk(尽管还有其他替代方案)通过端口 80 而不是 8080 后面的 Web 服务器为 api 提供服务,这将解决跨域问题。

This is common practice, have the 'static' content in the webserver and dynamic content in the container, but both served from behind the same domain.

这是常见的做法,在网络服务器中拥有“静态”内容,在容器中拥有动态内容,但两者都来自同一个域的后面。

The url for the rest api would be http://localhost/restws/json/product/get

其余 api 的 url 将是 http://localhost/restws/json/product/get

Here a description on how to use mod_jk to connect apache to tomcat: http://tomcat.apache.org/connectors-doc/webserver_howto/apache.html

这里描述了如何使用 mod_jk 将 apache 连接到 tomcat:http: //tomcat.apache.org/connectors-doc/webserver_howto/apache.html

回答by Amit

I think there is no need to specify

我认为没有必要指定

'http://localhost:8080`" 

in the URI part.. because. if you specify it, You'll have to change it manually for every environment.

在 URI 部分......因为。如果您指定它,则必须为每个环境手动更改它。

Only

仅有的

"/restws/json/product/get" also works