比方说,我想打电话给一些服务器的方法,并把它传递以下JSON对象:
Let's say I want to call some server method and pass it the following JSON object:
var t = { "test": 0};
我用用下面的选项jQuery库$阿贾克斯方式:
I'm using jQuery library $.ajax method with the following options:
type: "POST",
async: true,
url: 'mypage.aspx?Action=myAction,
data: {
test: JSON.stringify(t, null, 2)
},
contentType: 'application/x-www-form-urlencoded',
dataType: 'json',
.
.
.
在服务器端获取我用下面的code中的数据:
On the server side I fetch the data using following code:
public string GetInputStream()
{
string inputContent;
using (var sr = new System.IO.StreamReader(Request.InputStream))
inputContent = sr.ReadToEnd();
return Server.UrlDecode(inputContent.Split('=')[1]);
}
当从其他浏览器比苹果的Firefox版本3.6.12,getInputStream方法返回名为:
When called from browsers other than Mac Firefox version 3.6.12, getinputstream method returns:
test=%7B%0A++%22test%22%3A+0%0A%7D
这是有效的,然后可以反序列化对象,但调用的时候 从Mac OS X 10.5.8的Firefox 3.6.12这个方法我不能反序列化的字符串:
which is valid and can then be deserialized into an object, but when calling this method from Mac OS X 10.5.8 Firefox 3.6.12 I got a string that cannot be deserialized:
test=%7B%0A++%22test%22%3A0%0A%Pr
我相信,在参数在字符串的结尾是搞乱了我,而不是变成一个右括号。任何想法?
I believe that the Pr at the end of the string is messing me up and not turning into a closing bracket. Any ideas?
编辑:
我一直在寻找的萤火虫网>通话> POST选项卡,惊讶地看到,POST字符串是有效的:测试=%7B%0A ++%22test%22%3A%220%22%0A%7D
还有什么地方可以这个帖子的字符串进行修改,在服务器与一个无效的字符串的到来?
I was looking at the Firebug Net > Call > POST tab and was surprised to see that the POST string is valid: test=%7B%0A++%22test%22%3A%220%22%0A%7D
where else can this POST string be modified to arrive at the server with an invalid string?
EDIT2: 有趣的发现,一切问题都如果HTTPS(安全)的:-)用于解决。 我想这一定是在Mac上的Firefox中的安全设置?
Interesting finding, all problems are resolved if HTTPS (secure) is used :-). I guess it must be a security setting on Mac Firefox?
我觉得这是JQuery在Firefox AJAX错误在Mac上使用"管理和QUOT;用户
现在的问题是Firefox和在Mac OS X 10.5的家长控制功能之间的冲突。 POST数据的最后两个字符将被替换为镨。这个问题被首次报道,两年前,它看起来并不像它会被修复。
The problem is a conflict between Firefox and the parental controls in Mac OS X 10.5. The last two characters of the POST data are replaced with Pr. The problem was first reported two years ago, and it doesn't look like it will be fixed.
最好的工作,围绕我所知道的是,使用SSL,因为这似乎是prevent问题。如果这不是一个选项,那么你需要至少2个字符添加到您的POST数据的末尾,可忽略不计。在一个形式,这将意味着增加类似<输入类型=隐藏名称无用的价值=无用>
的形式结束(信贷米克尔Botanch )。对于JSON / XML请求,最后几个人物是解析重要。如果你不能只是添加空格或新行(也许这会的工作,我没有尝试过),那么你需要添加一些其他的虚值的客户端。最后,服务器端你需要检测并删除这个多余的虚拟值(如克鲁尔的回答一样)。
The best work around I am aware of is to use SSL because that seems to prevent the problem. If that isn't an option, then you need to add at least 2 characters to the end of your POST data which can be ignored. In a form that would mean adding something like <input type="hidden" name "useless" value="useless">
to the end of the form (credit to Miquel Botanch). For JSON/XML requests, the last few characters are important for parsing. If you can't just add spaces or new lines (maybe that would work, I haven't tried), then you need to add some other dummy value client side. Finally, server side you'd need to detect and remove this extra dummy value (as krul's answer does).