I’m working with the Google Maps API and picked up the book “Beginning
Google Maps Applications with Rails & AJAX”. I have encountered an
interesting problem.
I’m working in chapter 3 (for anyone who has the book) and it shows how
to build a rails app that allows for users to create string-tagged
markers and save them to a database.
I built the app and it works fine when I run it locally in Firefox.
However, if I run it locally in IE, something goes wrong and I get my
error message from the try/catch block.
I uploaded the app to my website to test it there and it does not work
in either Firefox or IE. You can see it here:
http://www.map.nvsagebrush.com
Has anyone encountered this before and now what I’m doing wrong?
The application.js contains the javascript necessary to manipulate the
google map while the controller saves the marker’s coordinates and
related string to the database.
Here is the code in question:
application.js
function storeMarker(){
var lng = document.getElementById(“longitude”).value;
var lat = document.getElementById(“latitude”).value;
var getVars = "?m[tag]=" + document.getElementById("tag").value
+ "&m[lng]=" + lng
+ "&m[lat]=" + lat ;
var request = GXmlHttp.create();
//call the store_marker action back on the server
request.open('GET', 'create' + getVars, true);
request.onreadystatechange = function() {
if (request.readyState == 4) {
//the request is complete
var success=false;
var content='Error contacting web service';
try {
//parse the result to JSON (simply by eval-ing it)
res=eval( "(" + request.responseText + ")" );
content=res.content;
success=res.success;
}catch (e){
success=false;
}
//check to see if it was an error or success
if(!success) {
alert(content);
} else {
//create a new marker and add its info window
var latlng = new
GLatLng(parseFloat(lat),parseFloat(lng));
var marker = createMarker(latlng, content);
map.addOverlay(marker);
map.closeInfoWindow();
}
}
}
request.send(null);
return false;
}
##################################
map_controller.rb
class ChapThreeController < ApplicationController
def create
marker = Marker.new(params[:m])
if marker.save
res={:success=>true,:content=>“
#{marker.tag}
else
res={:success=>false,:content=>“Could not save the marker”}
end
render :text=>res.to_json
end
def list
render :text=>(Marker.find :all).to_json
end
end
##################################