我想先说明一下:我是 JavaScript 的新手。我正在尝试使用 Leaflet 和 AJAX 调用发布用户位置和 map 边界。在我的事件处理程序 stateUpdater.onLocationFound 中,日志语句打印出正确的用户坐标和 map 边界,但是我在尝试时得到 Uncaught TypeError: Cannot read property 'lat' of undefined使用 $.param() 序列化这些值。我正在使用 Leaflet v0.7.2 和 jQuery 1.11.0。
var map;
$(document).ready(function() {
map = new L.map('map').setView([41.52, -71.09], 11);
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18
}).addTo(map);
map.on('locationfound', stateUpdater.onLocationFound);
stateUpdater.poll();
});
var stateUpdater = {
errorSleepTime: 10000,
poll: function() {
map.locate({setView: true, maxZoom: 18});
},
onLocationFound: function(e) {
//This log statement produces the correct output
console.log(e.latlng.toString());
var bounds = map.getBounds();
//As does this one
console.log(bounds.toBBoxString())
var args = {
"map_ne": bounds.getNorthEast(),
"map_sw": bounds.getSouthWest(),
"user_coords": e.latLng
};
//Uncaught TypeError thrown here
$.ajax({url: "/a/state/updates", type: "POST", dataType: "text",
data: $.param(args),
success: stateUpdater.onSuccess,
error: stateUpdater.onError});
},
onSuccess: function(response) {
console.log(response);
stateUpdater.errorSleepTime = 10000;
window.setTimeout(stateUpdater.poll, 10000);
},
onError: function(response) {
stateUpdater.errorSleepTime *= 2;
console.log("Poll error; sleeping for", stateUpdater.errorSleepTime, "ms");
window.setTimeout(stateUpdater.poll, stateUpdater.errorSleepTime);
},
};
对于可能导致此问题的原因,我没有任何预感,因此非常感谢您的帮助。
最佳答案
似乎 bounds.getNorthEast()、bounds.getSouthWest() 和 e.latlng 返回的对象还包含一些函数到 lat,lng 坐标,比如 equal() 和 toString() 防止 $.param()正确执行数据序列化并导致错误,这是从这些对象中仅获取所需内容的好方法:
var ll= $.extend({},{lat:e.latlng.lat, lng:e.latlng.lng}),
neBounds = bounds.getNorthEast(),
neBoundsLl = $.extend({},{lat:neBounds.lat, lng:neBounds.lng}),
swBounds = bounds.getSouthWest(),
swBoundsLl = $.extend({},{lat:swBounds.lat, lng:swBounds.lng}),
args = {
"map_ne": neBoundsLl,
"map_sw": swBoundsLl,
"user_coords": ll
};
我测试了这段代码,它对我有用,错误消失了,ajax 请求发送成功。
关于javascript - 未捕获的类型错误 : Cannot read property 'lat' of undefined when serializing leaflet data with $. 参数(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22317655/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串