noccer noccer - 2 days ago 5
Javascript Question

Passing large JavaScript object to C# backend without using JSON/strings (using IE11)

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.

Answer

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.

Comments