0

I have found a geocoder website that will give me some data that I need as a variable in my javascript code: http://geocoder.us/service/csv/geocode?zip=95472. the website returns only the content: 38.393314, -122.83666, Sebastopol, CA, 95472. I need pull this information from the website and put it into a string.

abc = "38.393314, -122.83666, Sebastopol, CA, 95472"

How can I accomplish this?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • exact duplicate of [Reading external plain text content into string](http://stackoverflow.com/questions/10312561/reading-external-plain-text-content-into-string) – Bergi Apr 26 '14 at 12:33

2 Answers2

2

You can use AJAX:

var req = new XMLHttpRequest(); //Create an AJAX object
req.open('GET','http://geocoder.us/service/csv/geocode?zip=95472',true); //Location and method
req.send(); //Send
req.onreadystatechange = function() { //When it's ready
    if (this.readyState === 4) { //... which is code 4
        console.log(this.responseText); //Then you have the responseText
    }
}

This only works when the request is from the same domain, tho (for security purposes). If you want it to work on any domain, you'll have to use a proxy.

bjb568
  • 11,089
  • 11
  • 50
  • 71
1

You should use Javascript to make an ajax request to that URL and it will return the information you want in a format you specify, usually JSON. Depending on what Javascript libraries you are/aren't using, there's different ways you could do that -- probably the most common would be to use jQuery to make your request. Here's info on that API:

https://api.jquery.com/jQuery.get/

Sam Hanley
  • 4,707
  • 7
  • 35
  • 63
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – bjb568 Apr 25 '14 at 23:13
  • Point taken. Thanks for the tip! I'll add relevant info. – Sam Hanley Apr 25 '14 at 23:25