Ajax POST method converts my "+" value in a string to " " why?
I can开发者_Python百科t figure out why in ajax post "+" sign converts to " ".please explain ?
Use the encodeURIComponent()
function to turn your data in valid encoded data for the request:
xhr.open("POST", url, true);
xhr.send(encodeURIComponent(postdata));
It's how URL encoding works. If you want a plus sign it's %2B, but you should really just escape or encode the data you're sending to the server. Type "a+b c" in here.
"+" is the url encoded symbol for space. As such, when your post data is decoded the "+" is converted to a space.
This is because URL Encoding converts spaces to +
since spaces aren't valid in URLs.
Normally characters are converted to %
followed by two hex digits, but having +
instead of %20
makes URLs more readable.
If you encode your +
as %2B
that should work.
Chances are that you are using the +
sign in an URL, where it is rightly converted into a space, as +
is the URLEncoded representation of a space character.
Run escape()
on whatever value you are putting into your URL to get it into URL-encoded form.
That's just standard url encoding. Plus signs are converted to spaces on the server. If you want to pass a plus sign you need to escape it as %2b.
精彩评论