0

I can't return a value from this AJAX request, I tried but I'm getting undefined. Any one please help me on this

function coordinates(values){
 var cords;
    $.ajax({
         url: 'https://geocoder.ls.hereapi.com/6.2/geocode.json',
         type: 'GET',  
         dataType: 'jsonp', 
         jsonp: 'jsoncallback',
         async: false,
         data: {
           searchtext: values,
           gen: '9',
           apiKey: 's_WF6U2g60ucHbmnYIyuieeUWnkT0jshGf4mD33kpwI'
         },
         success: function (data) {

          var x = JSON.parse(JSON.stringify(data));
          cords=[x.Response.View[0].Result[0].Location.DisplayPosition.Latitude, x.Response.View[0].Result[0].Location.DisplayPosition.Longitude];
         }

       });
    return cords;
      }
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Harsha
  • 74
  • 1
  • 10

1 Answers1

-1

Make sure your return type is Json, And change dataType: 'jsonp', to dataType: "json", and add contentType: "application/json".

  • 1
    No - AJAXP is still asynchronous - you can't return a value synchronously from an asynchronous request – Jaromanda X Dec 13 '19 at 11:13
  • yeah tried that @Himeshparmar doesnt work!! – Harsha Dec 13 '19 at 11:14
  • Hi, welcome to SO. This question gets asked maybe 10 times a day (anecdotally) and it's almost always due to a misunderstanding of what "asynchronous" means, rather than json/jsonp. See the linked duplicate for more information. – freedomn-m Dec 13 '19 at 11:40