-1

This is my first Stack Overflow question as a newly-employed junior developer. I have done a lot of searching but can't seem to find an answer, apologies if I have missed it.

I have a couple of large JavaScript objects that I am looking to pass from the frontend of a web app to the C# backend. With the app I'm working on, we have been using JSON.stringify(object) to prepare data to pass to the backend. When it hits the backend we do ApplyMyData data = JsonConvert.DeserializeObject<ApplyMyData>(json); - no issues normally.

EDIT: Here's how we post:

$.ajax({
    type: 'POST',
    url: '/Controller/MyMethod',
    data: {
        json: JSON.stringify(myObject)
    },
    success: function (response) {
        if (response.success) {
            // yay
        } else if (response.success === false) {
            // nay
        } else {
            alert('Something unexpected happened');
        }
    }
});

'Why not just keep using JSON?' I hear you ask!

Well, that's tricky. Thanks to client requirements, we must support IE11, which seems to have a limit on the size of strings. Reference: What is the maximum possible length of a query string?

Therefore, the resulting JSON.stringify(object) is incomplete/invalid and when it's parsed, an error is thrown. We have no issues when users are on Chrome.

I have done up a quick fiddle to demonstrate this problem, try run it in Chrome and then IE11, then look at the console logs and you'll see what I am getting at: https://jsfiddle.net/8yx7bqjs/. IE11 truncates the string length at 1024 characters... sigh...

So to my questions:

  • Is there another way I can send the JavaScript object 'unmodified' -or- 'unstringified' to the backend instead?
  • Must all data sent to backend be in the form of a string?
  • Is there a workaround for IE11 that can let you work with strings longer than 1024 characters? I can't seem to find any.

Thanks in advance.

Community
  • 1
  • 1
noccer
  • 13
  • 1
  • 6
  • 1
    Why are you passing the value back as a query string and not as POST data? – Pointy Nov 29 '16 at 04:32
  • And yes, by the very nature of HTTP you're sending strings to the server. – Pointy Nov 29 '16 at 04:32
  • 3
    Also also, IE11 does not have a string length limit of 1024 characters. Don't make the mistake of thinking that `console.log()` always tells the truth; try `console.log(evenBiggerString.length);` just for fun. – Pointy Nov 29 '16 at 04:37
  • @Pointy, I had a further look and it looks like my JSON string is indeed longer than 1024, so that was throwing me off the scent. Will dig deeper in the controller and try find the error. Thanks for your reply. – noccer Nov 30 '16 at 22:35

2 Answers2

1

You can make mock ajax requests in Fiddle. heres what I looked at really quickly:

https://jsfiddle.net/8yx7bqjs/1/

$.ajax({    
    type: 'POST',
    url: '/echo/json',
    data: {
        "stuff": myArray
    }
}).done(function(res){
    console.log("success");
}).fail(function(res){
    console.log("test failed")
});

IE11 is showing no problems with actually sending the data.

Depending on the actual length of your string ASP.Net has a limitation of 102400 by default.

robjam
  • 969
  • 1
  • 11
  • 24
1

Thanks @robjam, this has helped me a lot. I'm using Raygun for error reporting and gives me with the POST form value. The form value is truncated in their console so I thought (probably wrongly) that the string was being truncated in the browser before it actaully gets POST-ed to the backend.

But looks like IE11 is stringifying just fine, maybe RayGun simply isn't printing out all the JSON in their error logging console which has thrown me off the scent.

Will investigate further, thanks.

noccer
  • 13
  • 1
  • 6