jQuery ajax 调用 REST 服务 [英] jQuery ajax call to REST service

查看:35
本文介绍了jQuery ajax 调用 REST 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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-jackson-with-resteasy/

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

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.

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

Here is the html page with the ajax call:

<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?

谢谢!

推荐答案

您正在从与您请求的主机不同的主机运行 HTML.因此,您会被同源政策屏蔽.

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.

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

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

在 JSON 中,您将返回:

In JSON, you are returned:

{a: 5, b: 6}

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

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

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

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

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-type to application/javascript.

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

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

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

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

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

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

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

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

这篇关于jQuery ajax 调用 REST 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆