none
Unable to call Locations API with Angular 2 JSONP RRS feed

  • Question

  • I am trying to invoke the below API from Angular 2

    https://dev.virtualearth.net/REST/v1/Locations/US/WA/98052/Redmond?key=BingMapsKeyHere
    &callback=JSONP_CALLBACK

    What I receive back is json but not the JSONP function unless I add &jsonp=__ng_jsonp__.__req0.finished to the query string. Adding &jsonp=__ng_jsonp__.__req0.finished looks like a hack.

    What is the proper way to call the Locations API from Angular 2 and get a JSONP Function object?

    Code:

      getAddress(requestParams: bingmodels.IGetAddressRequest): Observable<bingmodels.IGetAddressResponse> {
        let url = "https://dev.virtualearth.net/REST/v1/Locations/US/WA/98052/Redmond?key=BingMapsKeyHere
    &callback=JSONP_CALLBACK";
        return this.jsonp.get(url)
          .map(this.onSuccess);
      }
    


    Friday, November 11, 2016 7:28 PM

Answers

All replies

  • This looks to be like an Angular2 specific question that might be better suited posted on StackOverflow. Doing a quick look around this post appears to be related: http://stackoverflow.com/questions/38866407/angular-2-jsonp-injected-script-did-not-invoke-callback

    This post also looks related: http://blog.loganarnett.com/jsonp-in-angular-2/

    That said, I also recommend taking a look at the Bing Maps best practices as the URL format you are using for geocoding isn't the best one to use. You can find the best practices documented here: https://msdn.microsoft.com/en-us/library/dn894107.aspx


    [Blog] [twitter] [LinkedIn]

    Friday, November 11, 2016 9:37 PM
  • Sure, i will try to post in stackoverflow. I am able to make JSONP calls from an Angular 2 application to wikipedia service [code below]

    But for the bing maps API, unless I add jsonp=__ng_jsonp__.__req0.finished to the query string, the JSONP call does not work so I am thinking the bing maps API implementation of JSONP is different than wikipedia service.

    Do you happen to have any sample angular 2 code consuming any bing rest APIs? Just wondering if anyone were able to consume bing rest apis from angular 2 application.

    Thanks for pointing me to the best practices.

    search(term: string) {
        let wikiUrl = 'https://en.wikipedia.org/w/api.php';
        let queryString = `?search=${term}&action=opensearch&format=json&callback=JSONP_CALLBACK`;
    
        return this.jsonp
          .get(wikiUrl + queryString)
          .map(response => <string[]>response.json()[1]);
      }

    Friday, November 11, 2016 10:15 PM
  • Found the issue. You are adding a callback URL parameter which doesn't exist in the Bing Maps REST services. There is a JSONP parameter you are meant to use. For example:

    https://dev.virtualearth.net/REST/v1/Locations/US/WA/98052/Redmond?key=YOUR_BING_MAPS_KEY&jsonp=JSONP_CALLBACK

    Note that there is no standard URL parameter for specifying a JSONP callback that is used by all REST services.


    [Blog] [twitter] [LinkedIn]


    Saturday, November 12, 2016 12:36 AM
  • Hi Ricky,

    In Angular 2, we could specify the callback only using this parameter - callback=JSONP_CALLBACK. Internally the callback method name will be modified to something like __ng_jsonp__.__req0.finished and hence when a http call is made from angular 2, it is impossible to know the callback function name. 

    I understand that I need to add the jsonp parameter with the correct callback function, but as I said, the callback function name is completely arbitrary. 

    So far, I am not able to call the Bing Maps rest services from Angular 2 using this.jsonp.get because of this issue. 

    Is there a workaround for this? 

    Thursday, November 17, 2016 2:02 AM
  • Angular 2 is only looking for JSONP_CALLBACK, not "callback=" the URL parameter can be anything. JSONP does not have a standard URL parameter. Use &jsonp=JSONP_CALLBACK when using the Bing Maps REST services and it should work.

    If on the off chance that Angular 2 actually does need "callback=" too, this would be a bug in Angular 2 as that's not the wouldn't support a small subset of the JSONP services that are out there.


    [Blog] [twitter] [LinkedIn]

    Thursday, November 17, 2016 3:31 AM