使用 & 符号解析 POST 数据
一些背景知识:我正在使用 jQuery UI 可排序序列化方法,该方法会生成以下内容:
category[]=Value & One&category[]=ValueTwo&category[]=ValueThree
然后,我发出 Ajax 请求,将数据发送 (POST) 到 Web 服务。
我目前正在使用 HttpUtility.ParseQueryString 方法将数据推送到集合中,但是 & 出现了问题。因为它的结果是:“Value”(“& One”被切断)。
这看起来应该非常容易修复,但出于某种原因我却一片空白。保存“价值与一”价值的最佳方式是什么?
编辑:添加代码示例:
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: /(.*)/
});
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: /(.*)/
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jQuery UI 已损坏。 jquery.ui.sortable.js 第 411 行:
糟糕,有人忘记在转储到字符串之前对键和值部分进行
encodeURIComponent()
编码。jQuery UI is broken. Line 411 of jquery.ui.sortable.js:
Whoops, somebody forgot to
encodeURIComponent()
the key and value parts before dumping into the string.这是 POSTed 数据的问题,&应该是%编码(成%26)。空格应编码为“+”:
It is a problem of the POSTed data, the & should be %-encoded (into %26). And space should be encoded as "+":
在发布您的数据之前。您应该使用任何本机 JS 转义功能来转义各个值。
http://xkr.us/articles/javascript/encode-compare/
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/