json 脚本中的变量 url

发布于 2024-11-08 11:29:53 字数 2060 浏览 0 评论 0原文

混了几天就知道了。学习 javascript 和 jquery 几周,进展顺利,但有时......

对于移动应用程序,我试图获取坐标。在页面上显示它们不是问题,但我希望它们在其他地方。

在 main.js

var getLocation = function() {
    var suc = function(p) {
        document.getElementById("locatie").innerHTML = "http://www.192.168.1.111/tools/gpslocation.php?lat=" + p.coords.latitude + "&lon= " + p.coords.longitude + "&max=20";
  };

    var locFail = function() {
    };
    navigator.geolocation.getCurrentPosition(suc, locFail);
};

和 htmlfile 中

<body  onload="getLocation();" >


     <p id="locatie">Finding geolocation...</p></ul>

<div id="geolocation">
Bezig met laden. Momentje geduld</div>
<script type="text/javascript">
jQuery(function(){

 var script=document.createElement('script');
 script.type='text/javascript';
 script.src= "http://www.192.168.1.111/tools/gpslocation.php?lat=53.216493625&lon=6.557756660461426&max=20";
 $("body").append(script);
});

function processTheseTerraces(jsonData){
var shtml = '';
 var results = jsonData.results;
 if(results){
 $.each(results, function(index,value){
 shtml += "<li class='store'><a class='noeffect' href='#'><span class='image' style='background-image: url(pics/terras1.jpg)'></span><span class='comment'>" + value.address + "</span><span class='name'>" + value.building_name + "</span><span class='stars5'></span><span class='starcomment'>132 Beoordelingen</span><span class='arrow'></span></a></li>";
 });

 $("#geolocation").html( shtml );
 }
}
</script>

现在我想要通过 json 传递坐标并加载数据。我想改变

   script.src= "http://www.192.168.1.111/tools/gpslocation.php?lat=53.216493625&lon=6.557756660461426&max=20";

script.src= "http://www.192.168.1.111/tools/gpslocation.php?lat=" + p.coords.latitude + "&lon= " + p.coords.longitude + "&max=20";

但那不起作用。任何人都建议我如何解决这个问题。

Messing around for days know. Learning javascript and jquery a few weeks, it goes well, but sometimes...

For an mobile app i'm trying to get the coordinates. Showing them on page isn't a problem, but I want them elsewhere.

In the main.js

var getLocation = function() {
    var suc = function(p) {
        document.getElementById("locatie").innerHTML = "http://www.192.168.1.111/tools/gpslocation.php?lat=" + p.coords.latitude + "&lon= " + p.coords.longitude + "&max=20";
  };

    var locFail = function() {
    };
    navigator.geolocation.getCurrentPosition(suc, locFail);
};

And in the htmlfile

<body  onload="getLocation();" >


     <p id="locatie">Finding geolocation...</p></ul>

<div id="geolocation">
Bezig met laden. Momentje geduld</div>
<script type="text/javascript">
jQuery(function(){

 var script=document.createElement('script');
 script.type='text/javascript';
 script.src= "http://www.192.168.1.111/tools/gpslocation.php?lat=53.216493625&lon=6.557756660461426&max=20";
 $("body").append(script);
});

function processTheseTerraces(jsonData){
var shtml = '';
 var results = jsonData.results;
 if(results){
 $.each(results, function(index,value){
 shtml += "<li class='store'><a class='noeffect' href='#'><span class='image' style='background-image: url(pics/terras1.jpg)'></span><span class='comment'>" + value.address + "</span><span class='name'>" + value.building_name + "</span><span class='stars5'></span><span class='starcomment'>132 Beoordelingen</span><span class='arrow'></span></a></li>";
 });

 $("#geolocation").html( shtml );
 }
}
</script>

Now I want the coordinates passing through json and load the data. I thought to change

   script.src= "http://www.192.168.1.111/tools/gpslocation.php?lat=53.216493625&lon=6.557756660461426&max=20";

in

script.src= "http://www.192.168.1.111/tools/gpslocation.php?lat=" + p.coords.latitude + "&lon= " + p.coords.longitude + "&max=20";

But that doesn't work. Anyone suggestions how I can solve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幸福%小乖 2024-11-15 11:29:53

这个:http://www.192.168.1.111只是一个错误的URL。我想你只需要这个:http://192.168.1.111

This: http://www.192.168.1.111 is just a wrong URL. I guess you need just this: http://192.168.1.111

左秋 2024-11-15 11:29:53

地理定位可能需要很长时间(几秒钟)。这是一个异步请求,这意味着其他 JavaScript 代码可能会在地理位置获取地址之前执行。解决方案是将任何使用位置的代码或函数调用放在 navigator.geolocation.getCurrentPosition 的回调函数中。JQuery

在定义 lat 和 lng 之前构建 URL。

Geolocation can take a long time (multiple seconds). It is an asynchronous request which means that the other javascript code may execute before the geolocation has grabbed the address. The solution is to put any code or function calls that use the location inside the callback function on the navigator.geolocation.getCurrentPosition

The JQuery is building the URL before the lat and lng have been defined.

白衬杉格子梦 2024-11-15 11:29:53

onload =“获取位置();”告诉您在加载文档时调用 getLocation 函数,并在该函数内将

标记的innerHTML 设置为:http://www.192.168。 1.111/tools/gpslocation.php?lat=" + p.coords.latitude + "&lon= " + p.coords.longitude + "&max=20

所以问题是:

  1. 如果您制作脚本标记并分配源,则浏览器会获取源数据,但在

    标记的内部 html 上写入 url 不会这样做没有意义。

  2. 下面的代码片段是在加载文档之前加载的,但我猜您不希望这样:

    jQuery(函数(){

    var script=document.createElement('script');
    script.type='文本/javascript';
    script.src=“http://www.192.168.1.111/tools/gpslocation.php?lat=53.216493625&lon=6.557756660461426&max=20”;
    $("body").append(脚本);
    });

  3. 如果你想要: script.src= "http://www.192.168.1.111/tools/gpslocation.php?lat=" + p.coords.latitude + "&lon= " + p。 coords.longitude + "&max=20"; 那么你必须先定义 p.coords,然后再调用它,否则 p.coords 是未定义的。


解决方案我不确定你到底问什么,所以无法回答。您想要分配 #locatie 元素的内部 HTML 还是要加载自定义脚本作为标签?

无论哪种方式,您都必须对服务器进行 ajax 调用,可能如下所示:

$.ajax({
  url: "http://www.192.168.1.111/tools/gpslocation.php",
  data: "lat=" + p.coords.latitude + "&lon= " + p.coords.longitude + "&max=20",

success: function(Result){
   // use Result variable in came from the success function.
   document.getElementById("Your_ID_Goes_Here").innerHTML = "do_Something";
  }
});

onload="getLocation();" tells that when the document is loaded call getLocation function and inside this function you set the innerHTML of <P id="locatie"> TAG as: http://www.192.168.1.111/tools/gpslocation.php?lat=" + p.coords.latitude + "&lon= " + p.coords.longitude + "&max=20

So problems are:

  1. If you make a script tag and assign source then browser fetch the source data but writing an url on inner html of a <p> tag won't do this and it doesn't make sense.
  2. Code fragment below is loaded before the document is loaded but i guess you do not want this:

    jQuery(function(){

    var script=document.createElement('script');
    script.type='text/javascript';
    script.src= "http://www.192.168.1.111/tools/gpslocation.php?lat=53.216493625&lon=6.557756660461426&max=20";
    $("body").append(script);
    });

  3. If you want: script.src= "http://www.192.168.1.111/tools/gpslocation.php?lat=" + p.coords.latitude + "&lon= " + p.coords.longitude + "&max=20"; then you have to define p.coords first and before calling this otherwise p.coords is undefined.


Solution i am not sure what you exactly asking so could not answer. Do you want to assign inner HTML of the #locatie element or do you want to load customized script as tag?

Either ways, you have to make an ajax call to server which maybe like this:

$.ajax({
  url: "http://www.192.168.1.111/tools/gpslocation.php",
  data: "lat=" + p.coords.latitude + "&lon= " + p.coords.longitude + "&max=20",

success: function(Result){
   // use Result variable in came from the success function.
   document.getElementById("Your_ID_Goes_Here").innerHTML = "do_Something";
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文