gpt4 book ai didi

javascript - 使用 & 从 JS 向 PHP 传递 URL 变量导致 "&"遗漏

转载 作者:行者123 更新时间:2023-11-30 11:10:15 25 4
gpt4 key购买 nike

为我找到这个问题的合适标题有点困难,所以也许这个例子会澄清我的问题。

我正在发出 ajax 请求以将一些变量从 JS 传递到 PHP。这些变量之一是带有一些选项的 URL,即

https://www.wondermap.it/cgi-bin/qgis_mapserv.fcgi?map=/home/ubuntu/qgis/projects/Demo_sci_WMS/demo_sci.qgs&SERVICE=WFS&VERSION=1.1.0&REQUEST=GetFeature&TYPENAME=impianti_risalita&

PHP 代码忽略第一个 & 符号后的任何选项,只考虑这部分

https://www.wondermap.it/cgi-bin/qgis_mapserv.fcgi?map=/home/ubuntu/qgis/projects/Demo_sci_WMS/demo_sci.qgs

我现在对 PHP 发出的 AJAX 请求看起来像

https://localhost/shire/php/export_wfs.php?wfs_url=https://www.wondermap.it/cgi-bin/qgis_mapserv.fcgi?map=/home/ubuntu/qgis/projects/Demo_sci_WMS/demo_sci.qgs&SERVICE=WFS&VERSION=1.1.0&REQUEST=GetFeature&TYPENAME=impianti_risalita&format=ESRI%20Shapefile

wfs_urlformat 是 PHP 应该处理的两个参数。

我想我应该避免将 & 符号放在 wfs_url 参数中,但我不知道应该怎么做。任何帮助将不胜感激。

编辑

这是 AJAX 调用:

var xhr;
if (window.XMLHttpRequest) xhr = new XMLHttpRequest(); // all browsers
else xhr = new ActiveXObject("Microsoft.XMLHTTP"); // for IE

// url is https://www.wondermap.it/cgi-bin/qgis_mapserv.fcgi?map=/home/ubuntu/qgis/projects/Demo_sci_WMS/demo_sci.qgs&SERVICE=WFS&VERSION=1.1.0&REQUEST=GetFeature&TYPENAME=impianti_risalita&
var php_url = window.location.protocol + "//" + window.location.hostname + '/shire/php/export_wfs.php?wfs_url=' + url + 'format=' + format_list[0];
xhr.open('GET', php_url, false);
xhr.onreadystatechange = function () {
if (xhr.readyState===4 && xhr.status===200) {
alert('Downloading...');
}
}
xhr.send();

return false;
});

最佳答案

以下是将其作为 POST 请求发送的方法:

var php_url = '/shire/php/export_wfs.php';
var formData = new FormData();
formData.append('wfs_url', url);
formData.append('format', format_list[0]);
xhr.open('POST', php_url);
xhr.onreadystatechange = function () {
if (xhr.readyState===4 && xhr.status===200) {
alert('Server reply: ' + xhr.responseText);
}
}
xhr.send(formData);

关于javascript - 使用 & 从 JS 向 PHP 传递 URL 变量导致 "&"遗漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53982282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com