0

I'm trying to get through $.get() function the content of an HTML page. This page contains in its filename the special character +:

mypage+v1.html

Here is how I'm trying to get the content:

$.get("mypage+v1.html", function(data){
    alert(data);
});

This code works and runs fine locally, it correctly retrieves the content. As soon as I move it to a web server, it stops to work and the ajax call returns 404 Not found error.

I tried to replace the + with an _ both in the script and in the filename and it works, but I need to keep the plus sign.. I tried to use encodeURI()

$.get(encodeURI("mypage+v1.html"), function(data){
    alert(data);
});

and also to manually replace + with %2B

var url = "mypage+v1.html".replace("+", "%2B");
$.get(url , function(data){
    alert(data);
});

but it didn't do the trick..

Using

$.get(encodeURIComponent("mypage+v1.html"), function(data){
    alert(data);
});

throws the following error:

GET http://.../mypage%2Bv1.html 404 (not found)

even with the encoded url it does not work.


Side note: I'm successfully retrieving other pages with

$.load("pagename.html #divToAppendTo", function(res, stat, xhr){
    ....
});

I need to use $.get because I need to manipulate and slice the file content before I append these slices in different portions of my original HTML file. I tested the $.load() call and it's using Request method: GET, this way I know that the folder is accessible..

Any other idea?

Thanks

BeNdErR
  • 17,471
  • 21
  • 72
  • 103
  • And if you replace + by %2B? – Adriien M Sep 08 '14 at 15:42
  • @AdriienM thanks for the reply: I tried your suggestion, it didn't work too.. (ps: answer updated) – BeNdErR Sep 08 '14 at 15:45
  • 2
    This sounds like a situation where you **should** never need to do what you're trying to do. Unless it's **completely** outside of your control, you should look into restricting the filenames allowed, as it's much easier to normalise those – Joe Sep 08 '14 at 15:49
  • @Joe unfortunately, it's **completely** outside of my control and I have to stick with that.. – BeNdErR Sep 08 '14 at 15:50
  • Can you retrieve the resource you want (`mypage+v1.html`) by writing its URL into the browser's address bar? Both a literal `+` and a `%2b` should work, technically, but if *manually* crafting the URL does not work you're out of luck with jQuery as well. – Tomalak Sep 08 '14 at 15:55
  • To confirm: mypage+v1.html is in the same location as the HTML where you are running this JQuery? E.g. `http://myserver.com/blah/jqueryget.html` is retrieving `http://myserver.com/blah/mypage+v1.html`? – mccannf Sep 08 '14 at 15:57
  • Tomalak yes, it should work! @mccanff index and mypage are in 2 different directories – BeNdErR Sep 08 '14 at 16:04
  • @BeNDerR, then you need to put a path in your URL. E.g. `/pathto/otherdir/mypage+v1.html`. – mccannf Sep 08 '14 at 16:05
  • @mccanf I omitted it in the question, but I am already providing the right path to the file :( – BeNdErR Sep 08 '14 at 16:09
  • If you use `encodeURIComponent` instead of `encodeURI` in your example above, does that change anything? – mccannf Sep 08 '14 at 16:15
  • @mccannf question updated. it encodes the URL, but it does not work.. I'm starting to think the problem could be related to some server setting.. – BeNdErR Sep 08 '14 at 16:23
  • You mentioned ajax as a tag...if you use php, could you use file_get_contents() instead and return the results through ajax? – The One and Only ChemistryBlob Sep 08 '14 at 16:29
  • I also added JQuery as a tag :) anyway I can't test it with php right now... – BeNdErR Sep 08 '14 at 16:32

0 Answers0