Reputation: 153
I am using XMLHttpRequest to read a file called "filename%3Fformat=json". I'm getting an error back saying zone.js:1382 GET http://localhost:3000/file%3Fformat=json 404 (Not Found).
If I change the request to "filename3Fformat=json" and change the filename to the same (basically remove the % from both the filename and the request, it works just fine ... but I need to be able to include % in the filename.
I added the setRequestHeader in desperation but that didn't seem to make any difference.
var requestAllStations = new XMLHttpRequest();
requestAllStations.onload = function ( ) {
if (requestAllStations.readyState == requestAllStations.DONE && requestAllStations.status == 200) {
// get json file content
var allFITStations = JSON.parse(requestAllStations.responseText);
console.log(allFITStations);
}
};
requestAllStations.open("get", "./file%3Fformat=json", true );
requestAllStations.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
requestAllStations.send();
Upvotes: 0
Views: 1004
Reputation: 153
So, I ended up adding a .replace(/%/g, "%25") to the requested filename. I'm guessing what was happening was my server was trying to decode %3F to a ? in the filename so I'm further encoding the filename to include "%" (%25).
filename = file?format=json
encodeURIComponent(filename).replace(/%3D/g, "=").replace(/%/g, "%25"))
Upvotes: 0
Reputation: 3750
Use Javascript's escape() function to encode the special characters.
requestAllStations.open("get", escape("./file%3Fformat=json"), true );
The string escaped string will look like:
./file%253Fformat%3Djson
Upvotes: 1