Search code examples
jqueryajaxserializationampersand

Parsing POST data with ampersand


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: /(.*)/
        });

Solution

  • 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.