简体   繁体   中英

$.ajax to call post rest service

ajax code used to call rest service

   $
.ajax({
    type : "post",
    url : 'http://service/status',
    success : function(dt) {
                    $.each(dt, function(key, val) {
            var tr = $('<tr></tr>');
            $.each(val, function(k, v) {
                $('<td>' + v + '</td>').appendTo(tr);
            });
            tr.appendTo("#tableID");
        });

    },
    error : function(msg) {
        alert(msg.responseText);
    }

});

i want to sent json data in request body can you please help me out on what to add in above code

If you really want to add something to the request headers then you'll need to use the "headers" option or the "beforeSend" event that jQuery exposes. See the documentation here: http://api.jquery.com/jQuery.ajax/

I'm guessing that what you really want is to add data to the body of the request and you can do that using the "data" parameter:

$.ajax({
    type : "post",
    url : 'http://service/status',
    data : {
        your: 'data here',
        someJSON: '{"foo":"bar","boo":"far"}'
    },
    success : function(dt) {
                    $.each(dt, function(key, val) {
            var tr = $('<tr></tr>');
            $.each(val, function(k, v) {
                $('<td>' + v + '</td>').appendTo(tr);
            });
            tr.appendTo("#tableID");
        });

    },
    error : function(msg) {
        alert(msg.responseText);
    }

});

If I'm mistaken and you really do want headers, then simply replace "data" with "headers" and you are good to go...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM