开发者

Problem AJAX(ing) JSON object on Mac Firefox version (3.6.12)

开发者 https://www.devze.com 2023-02-08 20:57 出处:网络
Let\'s say I want to call some server method and pass it the following JSON object: var t = { \"test\": 0};

Let's say I want to call some server method and pass it the following JSON object:

var t = { "test": 0};

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',
.
.
.

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]);
  }

When called from browsers other than Mac Firefox version 3.6.12, getinputstream method returns:

test=%7B%0A++%22test%22%3A+0%0A%7D

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?

EDIT: I was looking at the Firebug Net > Call > POST tab and was surprised to see that the POST 开发者_StackOverflowstring 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: Interesting finding, all problems are resolved if HTTPS (secure) is used :-). I guess it must be a security setting on Mac Firefox?


I've end up modifying GetInputStream() method to replace "%Pr" with closing bracket using regex:

 public string GetInputStream()
 {
    string inputContent;
    using (var sr = new System.IO.StreamReader(Request.InputStream))
       inputContent = sr.ReadToEnd();

    inputContent = System.Text.RegularExpressions.Regex.Replace(inputContent, "%Pr$", "}");

    return Server.UrlDecode(inputContent.Split('=')[1]);
}

I know it's not solving cause of the problem but at least I'm buying some time to come up with solution :-(


I think this is a duplicate of JQuery AJAX Error in Firefox on Mac with "Managed" Users

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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消