DavidW
DavidW

Reputation: 5129

URL breaking when it contains # in jQuery ajax request

When I request a url with jquery Ajax that contains # sign, the string of the url after # gets cut off. For example:

http://somesitem.com/?name=Some#thing

When I observe this url being requested in firebug, I see that all that is being submitted is:

http://somesitem.com/?name=Some

I tried to use encodeURI function with no success.

Any tips on how to submit the entire string containing the #?

Upvotes: 1

Views: 1117

Answers (5)

Paul
Paul

Reputation: 2530

http://api.jquery.com/category/selectors/

Special characters in JQuery documentation include #

It says use two \ to escape in JQuery (I believe only once in javascript).

Upvotes: 0

aknosis
aknosis

Reputation: 4308

The fragment identifier is never passed to the server. If you want to send it to the server you'd need to first convert the existing fragment to a standard url parameter.

var url = window.location;
//url = http://somesitem.com/?name=Some
url += '&hash='+window.location.hash;
//url = http://somesitem.com/?name=Some&hash=thing
$.get(url);

The fragment identifier functions differently than the rest of the URI: namely, its processing is exclusively client-side with no participation from the server — of course the server typically helps to determine the MIME type, and the MIME type determines the processing of fragments. When an agent (such as a Web browser) requests a resource from a Web server, the agent sends the URI to the server, but does not send the fragment. Instead, the agent waits for the server to send the resource, and then the agent processes the resource according to the document type and fragment value

http://en.wikipedia.org/wiki/Fragment_identifier

Upvotes: 0

mittmemo
mittmemo

Reputation: 2080

Try replacing '#' with '%23'. .

Upvotes: 1

DJ Sprouse
DJ Sprouse

Reputation: 83

encodeURI does not remove "#". You'll need to use encodeURIComponent

encodeURIComponent('Some#thing')

This should produce something like this:

Some%23thing

Upvotes: 2

Phillip Schmidt
Phillip Schmidt

Reputation: 8818

It's probably because the # character has a special meaning. It's basically a reserved character. I'd probably just suggest you use a different character for whatever you're using it for.

Upvotes: 0

Related Questions