Jquery.Ajax-如何传递数据
Jquery .Ajax - How to Pass Data
我正试图通过jquery ajax调用传递一个变量。我不知道该怎么做。我通过另一个html5脚本得到了lon-lat坐标。
如何获取另一侧的坐标?我试过$_GET(lat(。
我也不确定我是否能够在不同的<脚本>。
$.ajax({
cache: false,
url: "mobile/nearby.php",
dataType: "html",
data: "lat="+location.coords.latitude+"&lon="+loc.coords.longitude+,
success: function (data2) {
$("#nearbysgeo").html(data2);
}
});
这些脚本位于jquery代码之上
<script type="text/javascript">
google.setOnLoadCallback(function() {
$(function() {
navigator.geolocation.getCurrentPosition(displayCoordinates);
function displayCoordinates(location) {
var map = new GMap2(document.getElementById("location"));
map.setCenter(new GLatLng(location.coords.latitude, location.coords.longitude), 12);
map.setUIToDefault();
var point = new GLatLng(location.coords.latitude, location.coords.longitude);
var marker = new GMarker(point);
map.addOverlay(marker);
}
})
});
</script>
<script type="text/javascript" charset="utf-8">
function getLocation(){
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
} else {
document.getElementById("output").innerHTML = "Your browser doesn't handle the GeoLocation API. Use Safari, Firefox 4 or Chrome";
}
}
function success(loc){
console.log(loc);
strout = "";
for(l in loc.coords){
//strout += l +" = " +loc.coords[l] + "<br>";
}
strout += '';
strout += '<center><img src="http://maps.google.com/maps/api/staticmap?center='+loc.coords.latitude+','+loc.coords.longitude+'&markers=color:blue%7Clabel:Y%7C'+loc.coords.latitude+','+ loc.coords.longitude+'&zoom=15&size=400x250&sensor=false¢er=currentPosition"></center>';
document.getElementById("output").innerHTML = strout;
document.forms['newPostForm'].lat.value = loc.coords.latitude;
document.forms['newPostForm'].lon.value = loc.coords.longitude;
document.getElementById("coords").innerHTML = '';
document.getElementById("coords").innerHTML = 'CURRENT: Lat:' + loc.coords.latitude + ' Lon:' + loc.coords.longitude;
}
function error(err){
document.getElementById("output").innerHTML = err.message;
}
function clearBlog() {
document.getElementById("listview").innerHTML = '';
}
</script>
附加信息:
如果我用这条线就行了。所以我想我不能用这种方式使用loc.coords.latitude。
data: "&lat=43&lon=-79.3",
好吧,我现在黑了它,让它工作。我在页面上用lon和lat值填充了两个隐藏的表单元素。然后使用"document.forms['newPostForm'].lat.value"创建这样的行。
data: "&lat="+document.forms['newPostForm'].lat.value+"&lon="+document.forms['newPostForm'].lon.value,
仍然希望有一个实际的解决方案。
这是我正在处理的一个项目的一些代码。非常简单。
$.post("../postHandler.php", { post_action: "getRecentPosts", limit: "10" }, function(data){
$("#post-list").html(data);
你可以切换退出。发布。获得没有其他更改,比如:
$.get("../postHandler.php", { post_action: "getRecentPosts", limit: "10" }, function(data){
$("#post-list").html(data);
数据以这样的名称-值对传递。
{ post_action: "getRecentPosts", limit: "10" }
重写:
$.get("mobile/nearby.php", { lat: location.coords.latitude, lon: loc.coords.longitude }, function(data2){
$("#nearbysgeo").html(data2);
});
$lat = preg_replace('#[^0-9'.]#', '', $_GET['lat']);
如果location.coords.latitude
是以前定义的,那么您可能可以使用它。
jQuery.ajax(
{
url : 'mobile/nearby.php',
data : {
'action' : 'update',
'newname' : 'enteredText',
'oldname' : 'original_html',
'userid' : '10'
},
success : function(msg){
if(msg == 1)
{
alert('success');
}
}
});
这是jQuery.Ajax();
函数
相关文章:
- 如何将数据标签与数据一起传递,以便在高图中的工具提示中显示
- 如何调用另一个js文件中的函数或触发一个自定义事件,该事件将参数作为Jquery中的数据对象传递
- 将数据从 C# 传递到 angular js html
- 将选定的数据行传递到模式引导程序中
- 通过内联数据属性传递Javascript函数
- ajax调用中的数据未传递到控制器
- 在 MongoDB 中选择从数据上下文传递的字段
- 如何将表单输入数据从数据列表传递到我的角度控制器
- 如何将值从 asp.net 数据列表传递到 javascript 文本框
- 如何将数据从 c# 传递到 jquery/javascript
- 获取 JSON 数据并传递给指令 Angularjs
- 将JSON数据从C#传递到独立的Javascript文件(无Web服务器)
- 将字符串数据从 C# 传递到 JS
- Asp.net MVC 3过滤数据然后传递到JSON中
- 将相关的数据(道具)传递给后代
- 在JS后期调用中将GTM数据层传递给分析
- 将回调函数作为html数据属性传递
- 弹出窗口数据变量传递
- 将数据ajax传递到每个页面的同一个页面
- Laravel,Dingo Api和Node.js:数据不传递