0

As the answer to this question says, I have this block of code to query the best buy api:

$.ajax({
    type: "GET",
    url: "http://api.remix.bestbuy.com/v1/products(search=camera)?apiKey=" + apiKey + "&format=json&callback=?",
    cache: true,
    success: function(data) {
        alert('success');
    },
    dataType: 'json'
});

The code runs fine, but returns an error message from best buy:

"Couldn't understand '/v1/products(search=camera)?apiKey=myApiKey&format=json&callback=jQuery16209624163198750466_1312575558844'"

If I leave out "callback=?" the url returns products just fine when I go to it on the browser, but in the code it throws a javascript error:

"XMLHttpRequest cannot load http://api.remix.bestbuy.com/v1/products(search=camera)?apiKey=myApiKey&format=json. Origin http://mysite.com is not allowed by Access-Control-Allow-Origin."

Community
  • 1
  • 1
Juliet
  • 1,040
  • 3
  • 11
  • 18

3 Answers3

2

set dataType to jsonp

$.ajax({
    type: "GET",
    url: "http://api.remix.bestbuy.com/v1/products(search=camera)?apiKey=" + apiKey + "&format=json",
    cache: false,
    crossDomain:true,
    success: function(data) {
        alert('success');
    },
    dataType: 'jsonp',

});
Rafay
  • 30,950
  • 5
  • 68
  • 101
  • Doesn't work for me unfortunately. The code is running, but I'm getting the "couldn't understand.." error – Juliet Aug 05 '11 at 21:04
  • try setting the `crossDomain` to `true` – Rafay Aug 05 '11 at 21:25
  • Tried different combinations of crossDomain: true and datatype:jsonp or datatype:json and nothing worked. Still getting either of those two errors – Juliet Aug 05 '11 at 21:36
1

The Remix query parser can't handle an underscore in the JSON Callback. If you have a callback without an underscore it should work. Keep in mind, the Remix cache ignores the value of the JSON callback, so if the query is identical except the callback has changed, you will get a cached response (ie. the "couldn't understand . . ." error). Change the query slightly and you will get a fresh response.

ig65
  • 26
  • 1
1

Update: Found a solution, but it's not ideal. I'd rather not use php, but it works.

I have a php file that grabs the data:

$requestUrl="http://api.remix.bestbuy.com/v1/products(search=camera)?format=json&apiKey={$apiKey}";
$data=file_get_contents($requestUrl);
echo $data;

Then I grab that file with jquery:

$.ajax({
    url: "js/getBestBuy.php",
    dataType: "json",
    success: function(data) {
        alert('success');
    }
});
Juliet
  • 1,040
  • 3
  • 11
  • 18