44

I create a json that needs to be posted in jersey, a server running by grizzly that has a REST webservice gets incoming json object which need to be outputed. I'm giving a try but not sure how to implement this correctly.

import java.io.IOException;
import java.io.InputStream;

import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import org.apache.commons.io.IOUtils;

import javax.ws.rs.*;

    @Path("/helloworld")
    public class GetData {
        @GET
        @Consumes("application/json")
        public String getResource() {

            JSONObject obj = new JSONObject();
            String result = obj.getString("name");

            return result;      
        }                   

    } 

i have a html file that runs this method while onload

    function sendData() {
        $.ajax({
                url: '/helloworld',
                type: 'POST',
                contentType: 'application/json',
                data: {
                    name:"Bob",


                },
                dataType: 'json'
            });
            alert("json posted!");
        };

2 Answers 2

91

To send json to the server, you first have to create json

function sendData() {
    $.ajax({
        url: '/helloworld',
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({
            name:"Bob",
            ...
        }),
        dataType: 'json'
    });
}

This is how you would structure the ajax request to send the json as a post var.

function sendData() {
    $.ajax({
        url: '/helloworld',
        type: 'POST',
        data: { json: JSON.stringify({
            name:"Bob",
            ...
        })},
        dataType: 'json'
    });
}

The json will now be in the json post var.

7
  • Thank you! I'm still figuring out how the server get the json object.
    – nihulus
    Apr 11, 2012 at 18:05
  • When sent in this way, the json will be the request body. If you instead want the json to be in a POST var, the ajax request will need to be modified.
    – Kevin B
    Apr 11, 2012 at 18:14
  • But what if I want to Json object as it. i.e. Suppose I have JSONObject created using jsp and want to pass to next page then how to send JSONObject (jsonobj) in jquery POST
    – ajay_t
    Aug 2, 2013 at 9:33
  • @Optimus both ways do send the jsonobject in a post request. With the first, the jsonstring will be in the request body of the post request, and the second the jsonstring will be in a post variable named "json"
    – Kevin B
    Aug 2, 2013 at 14:49
  • 1
    only because your body parser doesn’t detect without it. but it’s not. a bad idea to have it
    – Kevin B
    Jun 4, 2018 at 13:59
4

It is also possible to use FormData(). But you need to set contentType as false:

var data = new FormData();
data.append('name', 'Bob'); 

function sendData() {
    $.ajax({
        url: '/helloworld',
        type: 'POST',
        contentType: false,
        data: data,
        dataType: 'json'
    });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.