Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 746629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:06:46+00:00 2026-05-14T14:06:46+00:00

I have been working on a shopping cart that the user can add/remove order

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:06:47+00:00Added an answer on May 14, 2026 at 2:06 pm

    I don’t see anywhere in this code where you access the d property of the response.

    Perhaps you mean to do this?

    $("#cartRow" + orderID).find(".subTotal").text(sTotal.d);
    // --------------------------------------------------^^
    

    EDIT

    Ok, I see the problem. You’re returning JSON but not defining a dataType in the $.ajax() call. This means that jQuery sees your application/json mimetype and interprets the response as JSON. 128.00 in JSON is a Number, not a String. However, "128.00" would be a String.

    In order to keep this working, You need to format the response before printing it (as others have suggested), or adjust your endpoint to return a valid JSON string.

    Here’s my test to prove the solution

    <div id="test">
      Subtotal <span class="subTotal"></span>
    </div>
    
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript" charset="utf-8">
      google.load("jquery", "1.4.2");
    </script>
    <script type="text/javascript" charset="utf-8">
    
    $.ajax({
      url: 'test.php',
      data: {},
      success: function(sTotal) {
        //order was updated: set span to new sub-total
        $("#test").find(".subTotal").text(sTotal);
      }
    });
    
    </script>
    

    and test.php

    <?php
    
    header( 'Content-type: application/json' );
    echo '128.00';
    

    Output

    Subtotal 128

    But when I change test.php to be this

    <?php
    
    header( 'Content-type: application/json' );
    echo '"128.00"';
    

    The expected output is generated

    Subtotal 128.00

    Or, you could alternatively tell jQuery to treat the response as text by specifying a dataType parameter, for example

    $.ajax({
      url: 'test.php',
      data: {},
      dataType: 'text',  // <---- here
      success: function(sTotal) {
        //order was updated: set span to new sub-total
        $("#test").find(".subTotal").text(sTotal);
      }
    });
    

    EDIT 2

    Ok, after messing with this some more, I see what’s going on. The dataFilter handler you defined converts the response into JSON itself, and in this case, returns the string 128.00. However, jQuery still applies the intellgent-guessed dataType (which is JSON) to this value before sending it to the success handler.

    There are a multitude of ways to fix this, all of which depend on what other AJAX calls your application relies on this setup for. The quick-fix I applied in my test was to do this

    $.ajaxSetup({
      type: "POST",
      contentType: "application/json; charset=utf-8",
      data: "{}",
      // define the text data type so that we return data.d, jQuery doesn't parse it as JSON again
      dataType: 'text',
      dataFilter: function(data) {
          data = $.parseJSON( data ); // Use jQuery's parsing
          if (data.hasOwnProperty('d'))
          {
              return data.d;
          }else{
              return data;
          }
      }
    });
    

    But that may not work across the board for you

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working with NSArray s and NSMutableArray s that store NSDate objects
I've been able to get my shopping cart working fine with PayPal when it's
I have been working with Core Data in an iPad app and I can
I have a simple shopping cart application that displays the session's shopping cart with
For a site with shopping cart we have https working on the domain.com/shop-2/cart location.
I have a working WCF service and worker role that I have been debugging
Have been working on this question for a couple hours and have come close
I have been working with SQL Server as a Developer a while. One thing
I have been working on a large java application. It is quite parallel, and
I have been working on this app for at least 3-4 months and just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.