XMLHttpRequest automatically replaces backslash (\) with slash (/)

倾然丶 夕夏残阳落幕 提交于 2020-01-05 09:11:53

问题


I am trying to run a small javascript script. one of the parameters of the XMLHttpRequest is a file path, so a URL would look like:

http://myaddress:myport/action/C:\\PATH\\TO\\MY\\FILE.EXT/some/other/params

however, XMLHttpRequest changes the address to:

http://myaddress:myport/action/C://PATH//TO//MY//FILE.EXT/some/other/params

which breaks the application. How can I prevent XMLHttpRequest from changing the requested address?


回答1:


Don't put raw special characters in the URL.

encodeURIComponent('C:\\PATH\\TO\\MY\\FILE.EXT')
"C%3A%5CPATH%5CTO%5CMY%5CFILE.EXT"



回答2:


Escape the path before putting it into your URL:

'http://myaddress:myport/action/' + encodeURIComponent('C:\\PATH\\TO\\MY\\FILE.EXT') + '/some/other/params'


来源:https://stackoverflow.com/questions/9944251/xmlhttprequest-automatically-replaces-backslash-with-slash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!