Sunday, February 2, 2014

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:


    
I can't figure it out where I went wrong, could you please tell me what i am doing wrong?
Thanks!
share|improve this question
1 
Is your site running on http://localhost:8080 as well? If it's not (http://localhost/ is not the same), you'll have to make your API output JSONP. –  Rocket Hazmat Aug 18 '11 at 19:02 
 
no, it's just a simple html, it's running on double click :D –  DaJackal Aug 18 '11 at 19:05
 
I meant, what is the URL that points to this page? –  Rocket Hazmat Aug 18 '11 at 19:06 
1 
You are getting blocked by the same origin policy, you'll need to use JSONP. –  Rocket Hazmat Aug 18 '11 at 19:08
 
... or server your html from an HTTP server –  Jeremy Heiler Aug 18 '11 at 19:09
show 2 more comments

4 Answers

up vote35down voteaccepted
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 toapplication/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);
   }
});
share|improve this answer
 
it's perfecty working now, and i think i understood. thanks –  DaJackal Aug 18 '11 at 19:19
 
You're welcome. JSONP is kind of a 'hack' to get data from other URLs, it's pretty cool. –  Rocket HazmatAug 18 '11 at 19:19
 
i get a response now in firebug, but it doesn't enter in function(data). do you know why? –  DaJackal Aug 18 '11 at 19:21
 
@DaJackal: Did you remember to have the webservice wrap the JSON in a function call? –  Rocket HazmatAug 18 '11 at 19:22
1 
I did it after all, after understanding how query param in rest service works, thank you very much! – DaJackal Aug 18 '11 at 20:48
show 11 more comments

5 comments:

oakleyses said...

louis vuitton handbags, oakley sunglasses, louboutin, longchamp outlet, nike shoes, louis vuitton outlet stores, chanel handbags, burberry outlet, prada outlet, jordan shoes, tiffany and co, michael kors outlet, tory burch outlet, louis vuitton outlet, longchamp handbags, nike free, true religion jeans, michael kors outlet, kate spade outlet, polo ralph lauren outlet, tiffany and co, prada handbags, polo ralph lauren outlet, michael kors outlet, michael kors outlet, longchamp handbags, oakley sunglasses, ray ban sunglasses, kate spade handbags, burberry outlet, louis vuitton outlet, louboutin outlet, louboutin, coach factory outlet, air max, air max, coach outlet, gucci outlet, christian louboutin shoes, michael kors outlet, coach purses, ray ban sunglasses, michael kors outlet, louis vuitton, coach outlet store online, true religion jeans, oakley sunglasses cheap

oakleyses said...

ralph lauren, lululemon, air max, hollister, north face, nike air max, polo lacoste, vanessa bruno, timberland, vans pas cher, louboutin, louis vuitton, oakley pas cher, air max pas cher, nike roshe run, air max, true religion outlet, barbour, sac longchamp, air force, hollister, sac louis vuitton, nike free, polo ralph lauren, nike trainers, louis vuitton uk, nike roshe, sac hermes, longchamp, michael kors, sac burberry, sac guess, mulberry, new balance pas cher, converse pas cher, sac louis vuitton, hogan outlet, nike tn, north face, true religion outlet, ray ban pas cher, michael kors, air jordan, nike blazer, nike free pas cher, michael kors pas cher, abercrombie and fitch, ray ban sunglasses

oakleyses said...

ralph lauren, lululemon, air max, hollister, north face, nike air max, polo lacoste, vanessa bruno, timberland, vans pas cher, louboutin, louis vuitton, oakley pas cher, air max pas cher, nike roshe run, air max, true religion outlet, barbour, sac longchamp, air force, hollister, sac louis vuitton, nike free, polo ralph lauren, nike trainers, louis vuitton uk, nike roshe, sac hermes, longchamp, michael kors, sac burberry, sac guess, mulberry, new balance pas cher, converse pas cher, sac louis vuitton, hogan outlet, nike tn, north face, true religion outlet, ray ban pas cher, michael kors, air jordan, nike blazer, nike free pas cher, michael kors pas cher, abercrombie and fitch, ray ban sunglasses

oakleyses said...

mac cosmetics, mont blanc, marc jacobs, canada goose outlet, nike huarache, vans shoes, soccer jerseys, hollister, giuseppe zanotti, beats by dre, abercrombie and fitch, longchamp, insanity workout, celine handbags, bottega veneta, ghd, nfl jerseys, north face outlet, chi flat iron, ugg boots, birkin bag, ugg australia, canada goose, herve leger, ugg pas cher, rolex watches, valentino shoes, canada goose uk, canada goose, ferragamo shoes, canada goose, ugg boots, uggs outlet, north face jackets, soccer shoes, asics running shoes, new balance shoes, p90x, lululemon outlet, canada goose jackets, mcm handbags, instyler, babyliss pro, ugg, wedding dresses, jimmy choo outlet, reebok outlet, nike roshe run

oakleyses said...

parajumpers, karen millen, air max, converse, pandora charms, moncler, louboutin, moncler, links of london, lancel, juicy couture outlet, oakley, hollister, pandora charms, supra shoes, thomas sabo, canada goose, gucci, wedding dresses, timberland boots, swarovski crystal, air max, coach outlet store online, moncler, ray ban, canada goose, moncler, ugg, louis vuitton, swarovski, hollister, montre homme, moncler, hollister clothing store, ralph lauren, rolex watches, moncler outlet, moncler, iphone 6 cases, baseball bats, juicy couture outlet, toms shoes, vans, pandora jewelry, ugg, converse shoes