JQuery post JSON object to a server

后端 未结 2 1592
礼貌的吻别
礼貌的吻别 2020-11-27 04:36

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

2条回答
  •  情歌与酒
    2020-11-27 05:02

    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.

提交回复
热议问题