Ali Khalili Ali Khalili - 6 months ago 34
Ajax Question

ajax response deserialize before jquery parser

I have an api controller that would response a model with decimal property.
you know response type is just a simple string.
I know converting big decimal data to javascript number may lose some data.
so, I wanna convert a decimal(for instance number of varieties with more than 10) data to string before jquery json.parser ?


You've to develop jQuery Ajax Setup, angular $http interceptor, whatever, when you get response instance, you can do the following:

 if (response.body != null && typeof response.body === "string") {

      response.body = (response.body as string).replace(/:\s*(\d{14,}.\d{2,})\s*([,\}])/g, ':"$1"$2');

      // this will change "{ number : 214748364711111.2 }" to "{ number : '214748364711111.2' }"


Note that you've to change response before the underlying framework calls JSON.parse.

For jQuery this means $( document ).ajaxComplete method. More details at