通过&传递URL变量从JS到PHP的结果是“&"省略 [英] Passing URL variable with & from JS to PHP result in "&" omission

查看:92
本文介绍了通过&传递URL变量从JS到PHP的结果是“&"省略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为我找到这个问题的适当标题有点困难,所以也许这个例子可以阐明我的问题.

It is a bit difficult to find the proper title for this question for me, so maybe this example will clarify my issue.

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

I am making an ajax request to pass some variables from a JS to a PHP. One of these variables is a URL with some options, namely

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代码将忽略第一个& 符号后的所有选项,仅考虑此部分

The PHP code is ignoring any options after the first & symbol, considering only this part

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

此刻我向PHP发出的AJAX请求看起来像

The AJAX request to the PHP I am making at the moment looks like

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_url 和 format 是PHP应该处理的两个参数.

being wfs_url and format the two parameters the PHP is supposed to process.

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

I think i am supposed to avoid placing the & symbols in the wfs_url parameter, but I have no idea what should i do instead. Any help would be appreciated.

编辑

这是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请求发送的方法:

Here's how to send it as POST request:

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);

这篇关于通过&传递URL变量从JS到PHP的结果是“&"省略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆