2

I am trying to execute bing map REST API using JQuery AJAX method. The Rest URL shows correct results in browser. But when I execute same URL using AJAX method of JQuery it seems to be not working and I am getting following exception:

"Netowrk error: failed to execute send on xmlhttprequest"

Here is my code:

$.ajax({
        type: 'GET',
        async: false,
        dataType: 'text',
        url: 'http://dev.virtualearth.net/REST/v1/Locations/47.64054,-122.12934?includeEntityTypes=Address&includeNeighborhood=0&include=ciso2&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        success: function (result) {
            alert("working");
        },
        error: function (xhr, ajaxOptions, thrownError, request, error) {
            alert('xrs.status = ' + xhr.status + '\n' +
                  'thrown error = ' + thrownError + '\n' +
                  'xhr.statusText = ' + xhr.statusText + '\n' +
                  'request = ' + request + '\n' +
                  'error = ' + error);
        }
    });

When I open the same URL in browser I can see the result in JSON format.

Please help!!!

Azeem
  • 2,904
  • 18
  • 54
  • 89
  • 1
    Use dataType : 'JSON' in your ajax call – Sunil Sep 14 '15 at 12:27
  • 1
    Just a thought here but have you tried removing the asyn = false? Perhaps you have other ajaxcalls that get executed before this one is finished which give you this problem. Edit: After reading @sunil coment above i realized that his answer is more likely to be the soloution to your problem. Although i will leave the coment as an note that you can end up having problems when you have async = false while making several ajax calls. – ThunD3eR Sep 14 '15 at 12:27
  • @sunil I have changed dataType to JSON but still getting same error. Any thoughts? – Azeem Sep 14 '15 at 12:34
  • ajax would work only on the same server means if you give different url which have different server other than your existing server then it would not work – Harshit Sethi Sep 14 '15 at 12:41
  • @Coder2599 can you share it on jsfiddle? It seems difficult as you have your API key in a call,,but I can't sort it out by looking to your code only – Sunil Sep 14 '15 at 12:48
  • Is there any workaround if we would need to get data from external URLs? – Azeem Sep 14 '15 at 12:48
  • @sunil I want to read JSON response from this URL `http://dev.virtualearth.net/REST/v1/Locations/47.64054,-122.12934?includeEntityTypes=Address&includeNeighborhood=0&include=ciso2&key=Ar19hs9V6usMWGZWEyY9hiF5hu8JaOGJkOUL2hMhgyXUicCjar3UuzWrlSN_eFbK` – Azeem Sep 14 '15 at 12:52
  • @Coder2599, I tested it, issue is from server side that is CORS cross origin support, that's y u unable to make a API call. So try disabling CORS for your browser. If you are using Chrome, then you can find on google how to allow CORS in Chrome..It will solve your problem – Sunil Sep 14 '15 at 13:05
  • I was facing the same error when making a GET request (not to bing map. Just another api of our own server). Found out that I am sending a huge payload. Changed method from GET to POST. (The api server is also controlled by us. So could change method in backend). Problem was solved. – Md. Abu Nafee Ibna Zahid Nov 22 '20 at 10:23

3 Answers3

1

As mentioned in the other response you need to make the data type jsonp. However it should be a GET request to the Location service as cross domain POST requests are not supported. You can find a good blog post on using the Bing Maps services with various JavaScript frameworks like jQuery here: https://blogs.bing.com/maps/2015/03/05/accessing-the-bing-maps-rest-services-from-various-javascript-frameworks/

rbrundritt
  • 16,570
  • 2
  • 21
  • 46
1

I faced the same problem of the Failed to execute 'send' on 'XMLHttpRequest' (only appearing in Chrome) and finally found that the problem was on the async: false option used. I suppose this error arises because of trying to make synchronous request to foreign domains, which is not supported, as explained in jQuery's documentation:

Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation

Lluís Suñol
  • 3,466
  • 3
  • 22
  • 33
0

I had to decode your question a bit. It looks like you are using MS' virtualearth.net/Bing geolocation find by point API: https://msdn.microsoft.com/en-us/library/ff701710.aspx

Reading their documentation a bit, it looks like you are trying to send lat/long coordinates and receive address information back in return. You have set the includeNeighborhood flag to 0 in your URL, which means you don't that information (not sure if that was intentional).

As for the AJAX jQuery call, I think the error is in the call format. It's probably a type POST, because you are posting information to the Bing API and then receiving results. A GET request would be for a read-only API.

Example of how I would write this call (note the type and dataType parameters). You should send JSONP instead of JSON if want to use a callback function with the data you receive (likely scenario).

 $.ajax({
   type: 'POST',
   dataType: "JSONP",
   url: 'http://dev.virtualearth.net/REST/v1/Locations/47.64054,-122.12934?includeEntityTypes=Address&includeNeighborhood=0&include=ciso2&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
   success: function (resultPass) {
     console.log(resultPass);
   },
   error: function (resultFail) {
     console.log(resultFail);
   }
 });
serraosays
  • 7,163
  • 3
  • 35
  • 60
  • I modified code as per your guidelines and it seems the response is success but still I am getting exception/error in response message: `xrs.status = 200 thrown error = Error: jQuery1113033110253675520873_1442236288411 was not called xhr.statusText = success request = undefined error = undefined` – Azeem Sep 14 '15 at 13:13
  • Sometimes that error is related to an endpoint data response mismatch: http://stackoverflow.com/questions/14255502/error-jquery-was-not-called – serraosays Sep 14 '15 at 13:22
  • Tried JSON, and got this response: `xrs.status = 0 thrown error = xhr.statusText = error request = undefined error = undefined` – Azeem Sep 14 '15 at 13:31
  • Can you update the question with the revised call the error? Hard to read down here. – serraosays Sep 14 '15 at 13:32