Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery ajax call to REST service

Tags:

rest

jquery

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/

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.

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?

Thanks!

like image 419
DaJackal Avatar asked Aug 18 '11 19:08

DaJackal


People also ask

Is AJAX Call REST?

2 Answers. AJAX is a set of (typically) client-sided web development techniques, while REST is an architecture style for sending and handling HTTP requests. So you can use AJAX to send RESTful requests. A REST API is typically not implemented using AJAX, but can be accessed by an AJAX client.

Can we call API using AJAX?

AJAX (Asynchronous JavaScript and XML) is a set of tools used to make calls to the server to fetch some data. In this article, we will see how to implement a simple API call using AJAX. Prerequisites: Basic knowledge of AJAX and its function. You can learn all the basics from here.

How does AJAX return an API call?

The A in Ajax stands for asynchronous. That means sending the request (or rather receiving the response) is taken out of the normal execution flow. In your example, $. ajax returns immediately and the next statement, return result; , is executed before the function you passed as success callback was even called.


1 Answers

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.

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

In JSON, you are returned:

{a: 5, b: 6} 

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

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

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

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.

$.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({     type: "GET",    dataType: "jsonp",    url: "http://localhost:8080/restws/json/product/get",    success: function(data){              alert(data);    } }); 
like image 150
Rocket Hazmat Avatar answered Sep 21 '22 14:09

Rocket Hazmat