0

Im currently trying to use the OSRS api in jquery.

I have used it prior to this with httparty in rails but wanted to see if i could do it in a javascript way.

However i seem to be having a problem parsing the contence of the response despite it appearing valid.

My code can be seen below:

// jquery Stuff
$( document ).ready(function() {
  var url_test = "http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item=207&format=json";
  $.ajax({
    crossDomain: true,
    dataType: "jsonp",
    url: url_test ,
    }).done(function ( data ) {
      console.log(data);
      alert( "Load was performed." );
  });

});

This is the 'broken' responce i get from the api which throws the error of Uncaught SyntaxError: Unexpected token :

{"item":{"icon":"http://services.runescape.com/m=itemdb_oldschool/1492081307848_obj_sprite.gif?id=207","icon_large":"http://services.runescape.com/m=itemdb_oldschool/1492081307848_obj_big.gif?id=207","id":207,"type":"Default","typeIcon":"http://www.runescape.com/img/categories/Default","name":"Grimy ranarr weed","description":"It needs cleaning.","current":{"trend":"neutral","price":"7,338"},"today":{"trend":"negative","price":"- 20"},"members":"true","day30":{"trend":"positive","change":"+2.0%"},"day90":{"trend":"positive","change":"+8.0%"},"day180":{"trend":"positive","change":"+8.0%"}}}

Is there someway that i can load this data correctly. I think its because of using jsonp if i where to guess. However i cant seem to load the api threw json data type as it throws a CORS error. So im a little stuck and any help with this would be great thanks !

Morphasis
  • 1,343
  • 1
  • 12
  • 25

1 Answers1

0

What you're returning is JSON, not JSONP. Your response looks like

{"name" : "Joe"}   //json format

and jQuery is expecting something like this:

jQuery42534534636363({"name" : "Joe"})   //jsonp format

If you actually need to use JSONP to get around the same origin policy, then the server serving needs to be able to actually return a JSONP response.

If the same origin policy isn't an issue for your application, then you just need to fix the dataType in your jQuery.ajax call to be json instead of jsonp.

// jquery Stuff
$( document ).ready(function() {
  var url_test = "http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item=207&format=json";
  $.ajax({
    crossDomain: true,
    dataType: "json",
    url: url_test ,
    }).done(function ( data ) {
      console.log(data);
      alert( "Load was performed." );
  });

});

JSONP Example (a wrapping function)

JSON Example

Sayuri Mizuguchi
  • 5,250
  • 3
  • 26
  • 53
  • Is there any way to work around it because i dont own the server its not my endpoint and httparty in rails seemed to work for some reason. (formatting was messed up) but i can handle string manipulation. Any way i can get the responce and not parse it automatically. – Morphasis Apr 17 '17 at 19:17
  • You can simple add one plugin (allow-cross-origin) or you can try this tutorials from [stackoverflow](http://stackoverflow.com/questions/5584923/a-cors-post-request-works-from-plain-javascript-but-why-not-with-jquery) – Sayuri Mizuguchi Apr 17 '17 at 20:20