How can I use JQuery to post JSON data?

后端 未结 5 2157
无人及你
无人及你 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:25

    The top answer worked fine but I suggest saving your JSON data into a variable before posting it is a little bit cleaner when sending a long form or dealing with large data in general.

    var Data = {
    "name":"jonsa",
    "e-mail":"qwerty@gmail.com",
    "phone":1223456789
    };
    
    
    $.ajax({
        type: 'POST',
        url: '/form/',
        data: Data,
        success: function(data) { alert('data: ' + data); },
        contentType: "application/json",
        dataType: 'json'
    });

提交回复
热议问题