In an Ajax POST, do I need to urlencode parameters before sending?
I have some lengthy JSON text that I am sending back to the server via Ajax:
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", myVeryLongAJAXText.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {...}
http.send(myVeryLongAJAXText);
Do I need to change the last line to:
http.send(encodeURI(myVeryLongAJAXText));
or does the send method take care of 开发者_如何学运维that?
You need to encode them on the client, and decode them on the server. It will work undecoded, but it's less error prone, and safer to encode/decode.
Send doesn't offer that because the data being sent could be just a single integer, so calling UrlEncoding would introduce unneeded overhead.
You should encode them. Send doesn't do this for you.
精彩评论