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

I am trying to grab some API data from a website (specifically Yummly), and it looks like when I try to execute a JSONP request, I get JSON data. As a result, the message "Uncaught SyntaxError: UnexpectedToken:" appears.

Code that tries to do this:

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 tried converting JSON from JSONP by overloading dataType, however this did not lead to any other results than the above. I also tried changing the callback function, but when I get a syntax error, it does not go to the function. When I do not use JSONP and just use JSON, I get: "XMLHttpRequest cannot load Origin not allowed Access-Control-Allow-Origin.".

Any help would be appreciated, I'm struggling a bit with that.

+4
source share
1 answer

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

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

  • API URL must be

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

  • Must be a character ?? between / recipes and q (q is the parameter, / v 1 / api / recipes is the way)

  • You can then pass the callback parameter as usual with a JSONP call ($ .ajax with dataType: "JSONP", as you use above, should work)

Finally, your app_key is secret. Do not publish it on public sites such as Stack Overflow. I recalled it, go to https://developer.yummly.com to create a new one.

+9
source

Source: https://habr.com/ru/post/1447105/


All Articles