Azeem
Azeem

Reputation: 2924

JQuery AJAX Error failed to execute send on xmlhttprequest

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!!!

Upvotes: 2

Views: 15891

Answers (3)

Lluís Suñol
Lluís Suñol

Reputation: 3651

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

Upvotes: 1

rbrundritt
rbrundritt

Reputation: 18013

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/

Upvotes: 1

serraosays
serraosays

Reputation: 7899

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);
   }
 });

Upvotes: 0

Related Questions