Hearthstone

FREE
By omgvamp | Updated 14日前 | Entertainment
Popularity

9.7 / 10

Latency

2,205ms

Service Level

99%

Health Check

N/A

Back to All Discussions

Help with consuming the API using angularJS?

Rapid account: Msuarin
msuarin
8年前

Hi I was wondering if you can help with consuming this API through AngularJS. As an example, this is how I would consume a weather API from openweathermap.org:

$scope.weatherAPI = $resource(“http://api.openweathermap.org/data/2.5/forecast/daily”, { APPID: “822b2ab7395a69803904483f3b0c88a1”,
callback: “JSON_CALLBACK”}, { get: {method: “JSONP” }});

$scope.weatherResult = $scope.weatherAPI.get({ q: $scope.city, cnt: $scope.days });

The JSON result would be stored in $scope.weatherResult.

I tried this with the hearthstone API but I’m not getting any results:

$scope.hsAPI = $resource(“https://omgvamp-hearthstone-v1.p.mashape.com/cards/Ysera”, { APPID: “dVJU44ytXLmshouErewLVrdVLz7vp1iBmABjsnesQ5qSalYnqa”,
callback: “JSON_CALLBACK”}, { get: {method: “JSONP” }});

$scope.hsResult = $scope.hsAPI.get();

I’m fairly new to AngularJS and consuming APIs in general so I’m sorry if this is a dumb question. Thanks.

Rapid account: Msuarin
msuarin Commented 8年前

Thanks man! I changed a few things there but that worked for me!

Rapid account: Omgvamp
omgvamp Commented 8年前

Disregard my previous message. You’ll have to do it slightly different since you can’t pass your Mashape API key when using JSONP. This is the correct solution:

function card($scope, $http) {
    $http.get('https://omgvamp-hearthstone-v1.p.mashape.com/cards/Ysera?callback=JSON_CALLBACK', {
	    headers: { 'X-Mashape-Key': 'MASHAPE_API_KEY_GOES_HERE' }
    }).success(function(data) {
        console.log(data);
    });
}
Rapid account: Omgvamp
omgvamp Commented 8年前

Hi!

This should work:

$http.jsonp('https://omgvamp-hearthstone-v1.p.mashape.com/cards/Ysera?callback=JSON_CALLBACK')
.success(function(data) { 
    console.log(data); 
});

Of course you still have to pass your API key etc.

Join in the discussion - add comment below:

Login / Signup to post new comments