Main question: How do I retrieve content with a url like "http://www.example.com/index.html#foo" via jQuery.ajax()?
Note: The following works for scripts, which is what I was originally requesting:
var script = document.createElement('script');
script.type= 'text/javascript';
script.src= 'http://www.example.com/script.js#foo';
$('head').append(script);
And you can build your own request in the plain old ajax way using the method found here:
var xmlHttp = new XMLHttpRequest();
var url="http://www.example.com/index.html#foo";
xmlHttp.open("GET", url, true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", 0);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
alert(xmlHttp.responseText);
}
}
xmlHttp.send();
However, this just seems like something jQuery.ajax should support.
As for jQuery, I have the following:
$.ajax({
url: 'http://www.example.com/index.html#foo',
cache: true
});
However, the request that is actually made is to "http://www.example.com/index.html". If I have no control over the end point I'm working with, and it responds with completely different content if "#foo" is not in the url, this is bad.
Based on an answer to another question on SO, I tried the following
$.ajax({
url: 'http://www.example.com/index.html',
cache: true,
data: {
'': '#foo'
}
});
However, the url that is requested is then "http://www.example.com/index.html?=#foo" which adds the equals sign and the ? for the query string (things I don't want).
Another question indicated that you can encode the url and then decode it in the beforeSend function, but I'm not sure what I can do to modify the request url in that location.
Since I already have a work around, this question is academic rather than an incredibly important time sensitive end of the world question, but I'm still curious. I bet I'm missing something dumb.
Unfortunately I lost my other SO question references while writing this up. So apologies for that.
Thanks in advance for your help!