1

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

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&

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

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

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

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.

EDIT

Here is the AJAX call:

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;
});
umbe1987
  • 2,894
  • 6
  • 35
  • 63
  • 1
    Can you add the actual AJAX request code? – General_Twyckenham Dec 30 '18 at 23:33
  • 1
    Use a POST instead of a GET request. –  Dec 30 '18 at 23:38
  • @General_Twyckenham Did it! – umbe1987 Dec 30 '18 at 23:40
  • @ChrisG That could be a possibility, will work on it and come back with my finding. – umbe1987 Dec 30 '18 at 23:41
  • 1
    @umbe1987 Looks like you're not [URL Encoding](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI) the request. – General_Twyckenham Dec 30 '18 at 23:43
  • I believe first `&` should be `?` – skyboyer Dec 30 '18 at 23:47
  • @General_Twyckenham I tried both placing `encodeURI()` in front of the url varialbe (`encodeURI(url)`) and the entire php_url string (`encodeURI(window.location.protocol + "//" + window.location.hostname + '/shire/php/export_wfs.php?wfs_url=' + url + 'format=' + format_list[0])`) in separate attempts, but I didn't succeed. Is that what you suggested? – umbe1987 Dec 30 '18 at 23:53
  • @umbe1987 the encodeURI function "encodes special characters, except: , / ? : @ & = + $ # ", so doesn't help. Try base64_encoding, as I show you in my answer. I've used it many times – Ass3mbler Dec 30 '18 at 23:54
  • @skyboyer no, it's a call to a Web Feature Server (WFS). If you try it (of course, wondermap.it not localhost) you'll see that it's responding. – umbe1987 Dec 30 '18 at 23:55
  • sorry, while scrolling at mobile have missed `?` – skyboyer Dec 31 '18 at 00:11

2 Answers2

2

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);
  • That did it. Thank you for providing the code, I didn't know about `new formData()`. – umbe1987 Dec 31 '18 at 00:20
  • @umbe1987 See here: https://stackoverflow.com/questions/9713058/send-post-data-using-xmlhttprequest –  Dec 31 '18 at 00:21
1

try including this function (base64_encode):

var php_url = window.location.protocol + "//" + window.location.hostname + '/shire/php/export_wfs.php?wfs_url=' + base64_encode(url) + 'format=' + base64_encode(format_list[0]);

and on the server side:

$wfs_url = base64_decode($_GET['wfs_url']);
$format = base64_decode($_GET['format']);
Ass3mbler
  • 3,855
  • 2
  • 20
  • 18
  • Thanks for your answer. I decided to accept the other though for two reasons: first of all, trying your suggestions i received `base64_encode is not defined javascript`; second, I preferred the other method. I am sure I would have succeeded if I would have overcame the base64 problem, so thanks again for your precious effort. – umbe1987 Dec 31 '18 at 00:19
  • @umbe1987 thank you for your kind explanation. The error you are getting is probably due to the inclusion of the function I've linked. I understand your choice, and I'm glad you've solved the issue. Good work and regards! – Ass3mbler Dec 31 '18 at 00:48