JQuery post JSON object to a server

后端 未结 2 1572
礼貌的吻别
礼貌的吻别 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.

    0 讨论(0)
  • 2020-11-27 05:23

    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'
        });
    }
    
    0 讨论(0)
提交回复
热议问题