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 7980749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:10:18+00:00 2026-06-04T10:10:18+00:00

I will be passing a json string to a servlet via an ajax request

  • 0

I will be passing a json string to a servlet via an ajax request :

function add() {
    $.ajax({
    url: "pathToServlet" ,
    dataType: "text",
    data: ({
        name : 'myJsonString'
    }),
    success: function(data) {
            alert('returned!!');
    });
}

To build up this json string I have a listener which when fired appends a new piece of json to string :

var json = "";
json += "{ new json ..... }"

Is this the correct way to build up the jSon String ? Should I be using jQuery methods to create a json object(if they exist) and add elements to it and then convert the json object to a string instead of creating the json string myself ?

  • 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-06-04T10:10:19+00:00Added an answer on June 4, 2026 at 10:10 am

    What I would recommend doing is building up an object, and then when you’re ready to send it to the server, serialize the object via JSON.stringify.

    So for instance, you might have an object called data:

    var data = {};
    

    …to which you might periodically add properties:

    data.foo = "bar";
    data.stuff = {nifty: "stuff"};
    

    Or perhaps data is an array:

    var data = [];
    

    …to which you add things:

    data.push({nifty: "stuff"});
    

    Then, when you’re ready to send it:

    function add() {
        $.ajax({
        url: "<%=savePortlet%>" ,
        dataType: "text",
        data: {
            name : JSON.stringify(data)
        },
        success: function(data) {
                alert('returned!!');
        });
    }
    

    Because you’re passing an object into ajax, you don’t have to worry about URL-encoding the JSON string; jQuery will do it for you.

    JSON.stringify is defined as part of ECMAScript5 and suppoted natively by many browsers, but of course many of us have to support outdated versions of browsers. In those cases, you can get a “JSON shim” to add JSON.stringify to an environment that doesn’t have it. One of those is available from the originator of JSON, Douglas Crockford, on his github page.

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

Sidebar

Related Questions

Thanks, I am passing in the array via json now and setting datatype to
I'm passing form data to a PHP script for processing via JS( jQuery.ajax() ).
I'm using XML::XML2JSON in Perl to convert JSON data to XML, I am passing
I'm writing an Android application which will occasionally need to download a json string
Will using GetComInterfaceForObject and passing the returned IntPtr to unmanaged code keep the managed
Autotest will rerun all tests every time my tests start passing when they were
Will GHC perform tail-call optimization on the following function by default? The only weird
Will this code ever wait on the mutex inside the producer's void push(data) ?
I am trying to retrieve the values from the following url: http://rentopoly.com/ajax.php?query=Bo . I
How can I made a JSON string out of a collection in dart, as

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.