Parsing POST data with ampersand

北战南征 提交于 2019-12-24 09:19:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!