简体   繁体   English

XMLHttpRequest 的问题或者是加载 gpx 文件的服务器设置问题

[英]problems with XMLHttpRequest or is a server setting problem loading a gpx file

I'm Italian almost a newbie in javascripting and server side rule configuration.我是意大利人,几乎是 javascripting 和服务器端规则配置的新手。 I'm creating a webside page, and intend to use a part of code of gpxtruder.xyz.我正在创建一个网页,并打算使用 gpxtruder.xyz 的一部分代码。

I have permission of owner (Jim Denova jim@anoved.net) to use original code, as it's released under opensource license.我已获得所有者 (Jim Denova jim@anoved.net) 的许可,可以使用原始代码,因为它是在开源许可下发布的。

In index.html page code is following:在 index.html 页面代码如下:

    <form enctype="multipart/form-data" method="get" name="gpxform">
            <p><input type="radio" name="gpxsource" value="0" id="gpxsource_upload" onchange="return toggle(event, 0, 'gpxfile') && toggle(event, 1, 'gpxsample');" />
                <label for="gpxsource_upload">Upload GPX:</label>
                <input type="file" id="gpxfile" /><br />
               <input type="radio" name="gpxsource" value="1" id="gpxsource_sample" onchange="return toggle(event, 0, 'gpxfile') && toggle(event, 1, 'gpxsample');" checked />
                 <label for="gpxsource_sample">Sample GPX:</label>
                <select id="gpxsample">
                    <option value="0" selected>South Mountain</option>
                    <option value="1">Vestal XX (20k road race)</option>
                </select>
                <h1>Hello, my name is <span id="name"></span></h1>
              <script>
                    let gpxsource = "1"
                let name = gpxsource;
                document.getElementById("name").innerHTML = name;
              </script>

you could choose a pre selected track to show result or load your own gpx file您可以选择预先选择的曲目来显示结果或加载您自己的 gpx 文件

parameters are sent to gpxtruder.js file, follows part of code processing data first part of code参数被发送到 gpxtruder.js 文件,跟随部分代码处理数据第一部分代码

    if (radioValue(form.gpxsource) === 0) {
    // Assign a local URL to the file selected for upload
    // https://developer.mozilla.org/en-US/docs/Web/API/URL.createObjectURL
    var files = document.getElementById('gpxfile').value;
                window.alert(gpxfile);
    //var files = document.getElementById('gpxfile').files;
                window.alert(files);
    if (files.length === 0) {
        Messages.error('No GPX file selected.');
        return;
    }
//  upload_url = window.URL.createObjectURL(files[0]);
        upload_url = files
} else {
    if (parseInt(form.gpxsample.value) === 0) {


        upload_url = "https://gpxtruder.xyz/gpx/SouthMtn.gpx";
    } else if (parseInt(form.gpxsample.value) === 1) {

        upload_url = "https://gpxtruder.xyz/gpx/VXX.gpx";
    } else {
        return;
    }
}
window.alert(upload_url);
window.alert(options);
loader(options, upload_url);

if (radioValue(form.gpxsource) === 0) {
    window.URL.revokeObjectURL(upload_url);
}

second part of code第二部分代码

var loader = function(options, gpx_url) {
window.alert(gpx_url);
Messages.clear();

var req = new XMLHttpRequest();
req.onreadystatechange = function() {
    if (req.readyState === 4 /*&& req.status == 200*/) {

        if (!req.responseXML) {
            Messages.error("This doesn't appear to be a GPX file.");
            return;
        }
window.alert(req.responseXML);
        // Attempt to parse response XML as a GPX file.
        var pts = Parser.file(req.responseXML, options.zoverride, options.zconstant);
        if (pts === null) {
            return;
        }

        // If all is well, proceed to extrude the GPX path.
        g = new Gpex(options, pts);
    }
};

// submit asynchronous request for the GPX file
req.open('GET', gpx_url, true);
req.overrideMimeType("text/xml");
req.send();

}; };

my problem is stricktly connected to upload_url = "https://gpxtruder.xyz/gpx/VXX.gpx";我的问题与upload_url = "https://gpxtruder.xyz/gpx/VXX.gpx"; if I change url address and insert my server web space, where I hosted gpx files,even if I use original file, page answer "This doesn't appear to be a GPX file."如果我更改 url 地址并插入我的服务器 web 空间,我在其中托管 gpx 文件,即使我使用原始文件,页面也会回答“这似乎不是 GPX 文件。”

I didn't find were problem could be.我没有发现问题可能是。

Could be a javascript related problem?可能是与 javascript 相关的问题吗? (but code and file are exacly a dupe of original) or could be a server side configuration problem were gpx file is hosted? (但代码和文件完全是原始文件的复制品)或者可能是托管 gpx 文件的服务器端配置问题?

original file could be found可以找到原始文件

https://gpxtruder.xyz/index.html https://gpxtruder.xyz/index.html

https://gpxtruder.xyz/js/gpxtruder.js https://gpxtruder.xyz/js/gpxtruder.js

thanks for help, if any possible.感谢您的帮助,如果可能的话。

Problem was a server side configuration, exactly a: CORS header 'Access-Control-Allow-Origin' missing问题是服务器端配置,确切地说是: CORS header 'Access-Control-Allow-Origin' 缺失

I'm a newbie and didn't know how browser console works and how could be useful我是新手,不知道浏览器控制台如何工作以及如何有用

solved via .htaccess通过 .htaccess 解决

   <IfModule mod_headers.c>
     Header set Access-Control-Allow-Origin "*"
   </IfModule>

answer found on CORS header 'Access-Control-Allow-Origin' missingCORS header 'Access-Control-Allow-Origin' 上找到的答案缺失

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM