在我的HomeController我有以下方法:传递一个值从JsonResult方法,另一种方法
public JsonResult AjaxTest(Position postData)
{
Session["lat"] = postData.Lat;
Session["lng"] = postData.Long;
return Json("", JsonRequestBehavior.AllowGet);
}
我怎么能有纬度和经度在我的索引方法?
它是public async Task<ActionResult> Index()
,如果它很重要。
在被检索和传递用户的当前坐标视图中的脚本:
var x = document.getElementById("positionButton");
(function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
}());
function showPosition(position)
{
if (position == null) alert('Position is null');
if (position.coords == null) alert('coords is null');
$('#lat').text(position.coords.latitude);
$('#long').text(position.coords.longitude);
var postData = { Lat: position.coords.latitude, Long: position.coords.longitude };
$.ajax(
{
type: "POST",
contentType: "application/json; charset=utf-8",
url: "@Url.Action("AjaxTest", "Home")",
//dataType: "json",
data: JSON.stringify(postData)
});
}
我需要能有另一个位置用户的当前纬度/长的时间距离。 lat和lng被声明为公共变量,那么为什么它们在试图使用Index方法内的坐标时保持为0?
编辑,这里是指数方法:
public object x;
public async Task<ActionResult> Index()
{
x = Session["lat"];
return View(parkingLot);
}
你在相同的请求中访问它们? – JB06
我不确定,在Index加载并获取坐标后,它将它发送到JsonResult。 – crystyxn
尝试将postData作为对象传递,而不是字符串。数据:postData。另一件事要检查...你有没有在你的javascript上设置一个调试器来确保你的Lat和Long不是0? – Daryl