2

I am sending JSON from the server to client side. The JSON contains a long.

It appears that the number is being rounded or something because:

  • The server side number sent is: 1036647050030089506
  • The client side number received is: 1036647050030089500

Why is this happening and how can I fix this?

Server side code:

Post["team", true] = async (parameters, ct) =>
{
    var team = this.Bind<Team>();
    team.Id = 1036647050030089506;

    Console.WriteLine("Response: " + team.Id);
    return Response.AsJson(team);
};

Client side code:

$.ajax({
    url: '/api/team',
    type: 'POST',
    dataType: "json",
    success: function (response) {
        alert("Response: " + response.id);
    }
});
sazr
  • 24,984
  • 66
  • 194
  • 362
  • 1
    Woah, no... don't do that. The response is definitely json. The problem here is that, even though JSON can handle indefinitely large numbers, JavaScript can't, so you're encountering loss-of-precision issues when it tries to convert your very large value into a Float. Please se @JohnnyFun's answer for more details there. – jmar777 Jul 25 '15 at 04:30

1 Answers1

3

Looks like you'll want to send it as a string. See this question for more details about how js handles big ol' numbas. Spoiler alert: poorly...but I still love you javascript...still love you.

Community
  • 1
  • 1
JohnnyFun
  • 3,975
  • 2
  • 20
  • 20
  • +1, this is correct. To help illustrate, try the following: `var num = 1036647050030089506; console.log(num);` – jmar777 Jul 25 '15 at 04:34