views:

116

answers:

3

Hi all,

I have been working on a shopping cart that the user can add/remove order items as they please and am returning an updated sub-total via a webservice using jQuery $.ajax

Here is how I am calling the webservice and setting the sub-total with the response.

//perform the ajax call
$.ajax({
    url: p,
    data: '{' + s + '}',
    success: function(sTotal) {
        //order was updated: set span to new sub-total
        $("#cartRow" + orderID).find(".subTotal").text(sTotal);
    },
    failure: function() {
        //if the orer was not saved

        //console.log('Error: Order not deleted');
    }
});

The response I am getting seems perfectly fine:

{"d":"128.00"} 

When I display the total on the page it displays as 128 rather than 128.00

I am fully sure it is something very simple and silly but I am so deep into it now I need someone with a fresh brain to help me out!!

Cheers :)

EDIT

I am also using $.ajaxSetup to set the correct contentType:

$.ajaxSetup({
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{}",
dataFilter: function(data) {
    var msg;

    if (typeof (JSON) !== 'undefined' &&
    typeof (JSON.parse) === 'function')
        msg = JSON.parse(data);
    else
        msg = eval('(' + data + ')');

    if (msg.hasOwnProperty('d'))
        return msg.d;
    else
        return msg;
}

});

+1  A: 
Peter Bailey
'd' is used by ASP.NET AJAX to wrap the returned value.
Sean Kinsey
sorry, forgot to mention I am using ASP.NET AJAX!
TGuimond
Answer updated with more stuff ;)
Peter Bailey
Hey thanks for all the help. I updated my queston above to show how I am using jQuery.ajaxSetup() to set the correct content type. I must admit, I am a little lost!
TGuimond
Updated again =)
Peter Bailey
+2  A: 

That is because the value is treated as a number, while you want it treated as a string. When you are using '.. .text(sTotal)', you are actually calling the .toString() method on the Number object wrapping the primitive sTotal. And since this is a whole number, it displays it without decimals. You need to use a format the number as a string prior to calling .text(foo) for the number to be formatted like that.

This will give you two decimals

var a=1/3; 
a = a.toString(); 
switch(a.lastIndexOf(".")){ 
    case -1: 
        a+=".00";
        break; 
    case a.length-2: 
        a+="0"; 
        break; 
    default: 
        a=a.substring(0, a.indexOf(".") + 3); 
} 
alert(a); 
Sean Kinsey
Can you give me an example? I assume its not just:var totalString = msg.toString(); $("#cartRow" + orderID).find(".subTotal").text(totalString);
TGuimond
This is will give you two decimals<pre><code> var a=1/3; a = a.toString(); switch(a.lastIndexOf(".")){ case -1: a+=".00" break; case a.length-2: a+="0"; break; default: a=a.substring(0, a.indexOf(".") + 3); } alert(a);</code></pre>
Sean Kinsey
+1  A: 

Please try this:

 $("#cartRow" + orderID).find(".subTotal").text(sTotal.toFixed(2));

HTH

Raja
I had tried that previously with no luck. Same result.
TGuimond
Thats wierd...I tried it in JSFiddle and it worked perfect. Please check if you are doing a parseInt or parseFloat while loading the value to .subTotal through some other event. HTH
Raja
That was it! Wow, thanks man. I am letting a huge sigh of relief out as we speak..
TGuimond