简体   繁体   English

jQuery ajax调用REST服务

[英]jQuery ajax call to REST service

I'm trying to make an ajax call from jquery to a rest service. 我正在尝试从jquery到休息服务进行ajax调用。 The rest service used is right from a tutorial of mkyong's blog, this one: http://www.mkyong.com/webservices/jax-rs/integrate-jackson-with-resteasy/ 使用的其他服务正是来自mkyong博客的教程,这个: http ://www.mkyong.com/webservices/jax-rs/integrate-jackson-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! 谢谢!

You are running your HTML from a different host than the host you are requesting. 您从与请求的主机不同的主机运行HTML。 Because of this, you are getting blocked by the same origin policy . 因此,您将受到相同原始策略的阻止。

One way around this is to use JSONP . 解决这个问题的一种方法是使用JSONP This allows cross-site requests. 这允许跨站点请求。

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. 您需要编辑REST服务以接受名为callback的参数,然后使用该参数的值作为函数名称。 You should also change the content-type to application/javascript . 您还应该将content-type更改为application/javascript

For example: http://localhost:8080/restws/json/product/get?callback=process should 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. 在JavaScript中,您需要告诉jQuery使用JSONP。 To do this, you need to append ?callback=? 要做到这一点,你需要追加?callback=? to the URL. 到URL。

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

If you use $.ajax , it will auto append the ?callback=? 如果你使用$.ajax ,它会自动追加?callback=? if you tell it to use jsonp . 如果你告诉它使用jsonp

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

From the use of 8080 I'm assuming you are using a tomcat servlet container to serve your rest api. 从8080的使用我假设你使用一个tomcat servlet容器来为你的api服务。 If this is the case you can also consider to have your webserver proxy the requests to the servlet container. 如果是这种情况,您还可以考虑让您的Web服务器代理对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后面的Web服务器而不是8080来处理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. 这是常见做法,在Web服务器中具有“静态”内容,在容器中具有动态内容,但两者都在同一域后面提供。

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

I think there is no need to specify 我认为没有必要指明

'http://localhost:8080`" 

in the URI part.. because. 在URI部分..因为。 if you specify it, You'll have to change it manually for every environment. 如果您指定它,则必须为每个环境手动更改它。

Only 只要

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

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

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