G
G
g_s_e2016-08-29 19:56:23
Angular
g_s_e, 2016-08-29 19:56:23

What could be the problem of getting Yandex API data via angular $http.jsonp?

Good day to all! I'm new to angular and I'm having this problem. I'm trying to get data on the train schedule at the station in this way:

rTApp.controller('welcomeController', [ 'Stations','$scope','$http',function(Stations, $scope, $http ){
    $scope.city = {name: "Moscow"};
    $scope.setCity = function(address){
        $http.get('http://maps.googleapis.com/maps/api/geocode/json', {params: {address: address}})
            .success(function(data){
                url = 'https://api.rasp.yandex.net/v1.0/nearest_stations/?&apikey={key_value}&lat='+data.results[0].geometry.location.lat + '&lng=' + data.results[0].geometry.location.lng+'&distance=50&lang=ru&transport_types=train&format=json&callback=JSON_CALLBACK';
                $http.jsonp(url).
                success(function(data, status, headers, config) {
                    console.log(data);
                });

to which I get the answer Invalid callback name "angular.callbacks._0"
When trying to send a request in this way:
url = 'https://api.rasp.yandex.net/v1.0/nearest_stations/?&apikey={key_value}&lat='+data.results[0].geometry.location.lat + '&lng=' + data.results[0].geometry.location.lng+'&distance=50&lang=ru&transport_types=train&format=json';

The server returns data, but on the client it shows:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin is therefore not allowed access.
also tried this one:
function custom_callback(data) {
    console.log(data);
}
rTApp.controller('welcomeController', [ 'Stations','$scope','$http',function(Stations, $scope, $http ){...............
url = 'https://api.rasp.yandex.net/v1.0/nearest_stations/?&apikey={key_value}&lat='+data.results[0].geometry.location.lat + '&lng=' + data.results[0].geometry.location.lng+'&distance=50&lang=ru&transport_types=train&format=json&callback=custom_collback';
  $http.jsonp(url)

...............
}]);

The server gives the data and passes it to the custom_callback method, but since it is outside the controller, there is no possibility of normal data processing and transfer to the view.
I will be grateful for any help. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nicholas, 2016-08-30
@healqq

Well, if for some reason the standard Angular callback does not work, you can put some service or something else into your custom_callback through a closure where you want to save the data.

N
napa3um, 2016-08-30
@napa3um

Use JS-API instead of JSONP: https://tech.yandex.ru/maps/jsapi/ . If you want it through JSONP, then push your callback into the global scope:
window.custom_callback = custom_callback;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question