Created
June 27, 2012 15:20
-
-
Save maddockpa/3004769 to your computer and use it in GitHub Desktop.
Ajax and jquery get request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http://nominatim.openstreetmap.org/reverse?format=xml&lat=40.004&lon=-82.862&MaxResponse=1&zoom=17&accept-language=en-us&addressdetails=1 | |
function getFullURL() { | |
var myURL = "http://nominatim.openstreetmap.org/reverse?"; | |
var queryString0 = document.forms[0].lat.value; | |
var queryString1 = document.forms[0].lon.value; | |
var myData = "format=xml" + "&lat=" + encodeURIComponent(queryString0) + "&lon=" + encodeURIComponent(queryString1) + "&MaxResponse=1" + "&zoom=17" + "&accept-language=en-us" + "&addressdetails=1"; | |
var myCompleteURL = myURL + myData; | |
return myCompleteURL; | |
} | |
function getMyData() { | |
var myURL = "http://nominatim.openstreetmap.org/reverse?"; | |
var queryString0 = document.forms[0].lat.value; | |
var queryString1 = document.forms[0].lon.value; | |
var myData = "format=xml" + "&lat=" + encodeURIComponent(queryString0) + "&lon=" + encodeURIComponent(queryString1) + "&MaxResponse=1" + "&zoom=17" + "&accept-language=en-us" + "&addressdetails=1"; | |
return myData; | |
} | |
function submitform() { | |
//concatenate post request into single string myData | |
var myURL = "http://nominatim.openstreetmap.org/reverse?"; | |
var myCompleteURL = getFullURL(); | |
var myData = getMyData(); | |
alert(myURL); | |
var theLocation = document.getElementById("docs"); | |
var text = document.createTextNode(myCompleteURL); | |
theLocation.appendChild(text); | |
alert(myData); | |
$.ajax({ | |
dataType: "xml", | |
url: myURL, | |
type: "GET", | |
data: myData, | |
mimeType: "xml", | |
success: requestSuccess, | |
error: requestFailure | |
}) | |
//Post method with data. Protocol for success and failure. | |
} | |
function requestSuccess(data,textStatus, jqXHR) { | |
alert("1"); | |
var xmlData = data, | |
xmlDoc = $.parseXML(xmlData), | |
$xml = $( xmlDoc ), | |
$result = $xml.find( "road"); | |
alert( $xml.text() ); | |
$( "#docs" ).append( $result.text() ); | |
//alert(request.status); | |
} | |
function requestFailure(response) { | |
alert("An error occurred while communicating with the OpenLS service. Please try again."); | |
} | |
//]]> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment