How to get response data from XMLHttpRequest?


I was trying to get location by calling google maps API using javascript and wanted to know how to get response text/data after request is successfully sent.

Here is my javascript code:

if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(sendLocation);
        } else {
            alert("Geolocation is not supported by this browser.");
        }

        function sendLocation(position) {
            var latlng = { lat: position.coords.latitude, lng: position.coords.longitude };
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function (data) {
                if (xhttp.readyState == 4 && xhttp.status == 200) {
                    // need response here but data is not working
                    console.log(data)
                }
            };
            xhttp.open("POST", " http://maps.googleapis.com/maps/api/geocode/json?latlng=position.coords.latitude,position.coords.longitude&sensor=false", true);
            xhttp.send();
        }

How can I get it? Thanks


Asked by:- Sam
0
: 205 At:- 6/13/2018 8:36:55 AM
Javascript google maps reverse geocoding






1 Answers
profileImage Answered by:- manish

You can get response text by using  XMLHttpRequest.responseText; in your javascrip code, so you modified code will be as below

function sendLocation(position) {
            var latlng = { lat: position.coords.latitude, lng: position.coords.longitude };
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function (data) {
                if (xhttp.readyState == 4 && xhttp.status == 200) {
                    
                    console.log(xhttp.responseText;)
                }
            };
            xhttp.open("POST", " http://maps.googleapis.com/maps/api/geocode/json?latlng=position.coords.latitude,position.coords.longitude&sensor=false", true);
            xhttp.send();
        }

If you use XMLHttpRequest to get the content of a remote HTML webpage, the responseText property is a string containing the raw HTML. This could prove difficult to manipulate and analyze. There are three primary ways to analyze and parse this raw HTML string:

  1. Use the XMLHttpRequest.responseXML property as covered in the article HTML in XMLHttpRequest.
  2. Inject the content into the body of a document fragment via fragment.body.innerHTML and traverse the DOM of the fragment.
  3. RegExp can be used if you always know the content of the HTML responseTextbeforehand. You might want to remove line breaks, if you use RegExp to scan with regard to linebreaks. However, this method is a "last resort" since if the HTML code changes slightly, the method will likely fail.
2
At:- 6/13/2018 3:25:49 PM
Good xhttp.responseText works for me, there small typo error of semi-colon in your answer's code. 0
By : Sam - at :- 6/21/2018 6:53:46 PM





Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use