Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I interpret JSON if jQuery thinks it is receiving a JSONP request?

I am trying to grab some API data from a website (in particular Yummly) and it looks like when I try to do a JSONP request I receive JSON data. This results in an "Uncaught SyntaxError: UnexpectedToken:".

The code which tries to do this is:

var keywords = $('#input-text').val();
var url = "http://www.yummly.com/api/recipesq="+keywords+"&_app_id=<snipped-app-id>&_app_key=<snipped-api-key>&";
 $.ajax({                                                                            
      type: 'GET',
       url: url,
       dataType: 'jsonp',
       //dataType: 'jsonp json'
       success: function() { console.log('Success!'); },
       error: function(data, data2) { console.log(data); },
       //jsonp: false,
       //jsonpCallback: 'recipeGet'                                          
  });
});

I have tried to convert to JSON from the JSONP by overloading dataType, however this did not result in any different result than the above. I have also tried changing the callback function, but when I receive the syntax error it does not go to the function. When I do not use JSONP and just use JSON I receive a, "XMLHttpRequest cannot load Origin is not allowed by Access-Control-Allow-Origin.".

Any help would be appreciated, i've been struggling with this for quite a bit.

like image 226
RKuncewicz Avatar asked Jan 14 '23 23:01

RKuncewicz


1 Answers

I am one of the developers responsible for the Yummly API.

The Yummly API supports JSONP (the documentation explains how) but there are several problems with your request.

  1. The API url should be

    http ://api.yummly.com/v1/api/recipes (remove the space after http)

  2. There should be a '?' between /recipes and q (q is a parameter, /v1/api/recipes is the path)

  3. You can then pass a callback parameter as usual with a JSONP call ($.ajax with dataType: 'JSONP' like you are using above should work)

Finally, your app_key is secret. Please don't publish it on public sites like Stack Overflow. I've revoked it, please go to https://developer.yummly.com to generate a new one.

like image 140
Vadim Geshel Avatar answered Jan 28 '23 12:01

Vadim Geshel