How can I use JQuery to post JSON data?

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

    You're passing an object, not a JSON string. When you pass an object, jQuery uses $.param to serialize the object into name-value pairs.

    If you pass the data as a string, it won't be serialized:

    $.ajax({
        type: 'POST',
        url: '/form/',
        data: '{"name":"jonas"}', // or JSON.stringify ({name: 'jonas'}),
        success: function(data) { alert('data: ' + data); },
        contentType: "application/json",
        dataType: 'json'
    });
    

提交回复
热议问题