问题
A bit of background: I'm using the jQuery UI sortable serialize method which produces something along the following:
category[]=Value & One&category[]=ValueTwo&category[]=ValueThree
I then make an Ajax request to send the data off (POST) to a web service.
I'm currently using the HttpUtility.ParseQueryString method to push the data into a collection, but a problem arises with the & as it results in: "Value" ("& One" is cut off).
This seems like it should be incredibly easy to fix, but for some reason I'm drawing a blank. What would be the best way to preserve the value as "Value & One"?
Edit: Adding code samples:
Dim data As String = "category[]=Value & One&category[]=ValueTwo&category[]=ValueThree"
Dim httpPOSTData As System.Collections.Specialized.NameValueCollection
httpPOSTData = HttpUtility.ParseQueryString(data)
'Result: "Value ,ValueTwo,ValueThree"
'Desired Result: "Value & One,ValueTwo,ValueThree"
Javascript:
serializedSortOrder = $('#Categories').sortable('serialize',{
attribute:'data-category',
key:'category[]',
expression: /(.*)/
});
回答1:
jQuery UI is broken. Line 411 of jquery.ui.sortable.js:
if(res) str.push((o.key || res[1]+'[]')+'='+(o.key && o.expression ? res[1] : res[2]));
Whoops, somebody forgot to encodeURIComponent()
the key and value parts before dumping into the string.
回答2:
It is a problem of the POSTed data, the & should be %-encoded (into %26). And space should be encoded as "+":
category[]=Value+%26+One&category[]=ValueTwo&category[]=ValueThree
回答3:
Prior to posting your data. You should escape the individual values using any of the native JS escape capabilities.
http://xkr.us/articles/javascript/encode-compare/
来源:https://stackoverflow.com/questions/5346877/parsing-post-data-with-ampersand