Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript发布数据_Javascript_Xmlhttprequest - Fatal编程技术网

使用javascript发布数据

使用javascript发布数据,javascript,xmlhttprequest,Javascript,Xmlhttprequest,尝试在页面加载时使用javascript发布数据 坐标是生成的,但没有其他结果,我做错了什么。。。 任何帮助都将不胜感激 <p id="demo">Cords are displayed here...</p> <script> var x = document.getElementById("demo"); //this display the cords only to ensure script works function getLocation()

尝试在页面加载时使用javascript发布数据 坐标是生成的,但没有其他结果,我做错了什么。。。 任何帮助都将不胜感激

<p id="demo">Cords are displayed here...</p>
<script>
var x = document.getElementById("demo"); //this display the cords only to ensure script works

function getLocation() {
if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
} else { 
    x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = position.coords.latitude +" "+ position.coords.longitude; //this test if the cords are generated
lg=position.coords.latitude +" "+ position.coords.longitude;

var url = "navigator.asp"; // this page is linked to a database which saves the post
var params = "id=1&lg="+lg;
xhr.open("POST", url+"?"+params, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xhr.send(params);
}
</script>

您不需要在URL和xhr.send中同时发送参数。根据服务器脚本的期望,使用一个或另一个。但我不认为同时发送它们会造成问题。Barmar,你的评论让我意识到我的javascript技能已经非常生疏。我做了一些复习并重新编写了代码。你是对的,我不需要在xhr.send-plus中发送url,我将代码的post部分放在一个由函数shoPosition触发的函数中。我还通过一个变量将地理位置从shoPosition传递到新的post函数。现在效果很好。