How can I use JQuery to post JSON data?

后端 未结 5 2163
无人及你
无人及你 2020-11-22 07:46

I would like to post Json to a web service on the same server. But I don\'t know how to post Json using JQuery. I have tried with this code:

$.ajax({
    typ         


        
5条回答
  •  天命终不由人
    2020-11-22 08:15

    Base on lonesomeday's answer, I create a jpost that wraps certain parameters.

    $.extend({
        jpost: function(url, body) {
            return $.ajax({
                type: 'POST',
                url: url,
                data: JSON.stringify(body),
                contentType: "application/json",
                dataType: 'json'
            });
        }
    });
    

    Usage:

    $.jpost('/form/', { name: 'Jonh' }).then(res => {
        console.log(res);
    });
    

提交回复
热议问题