开发者

Best Buy API with AJAX & jQuery - errors

开发者 https://www.devze.com 2023-03-26 01:02 出处:网络
As the answer to this question says, I have this block of code to query the best buy api: $.ajax({ type: \"GET\",

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 开发者_如何学Pythonurl 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."


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',

});


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');
    }
});


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号