How to send JSON instead of a query string with $.ajax?

后端 未结 4 2138
一生所求
一生所求 2020-11-22 10:39

Can someone explain in an easy way how to make jQuery send actual JSON instead of a query string?

$.ajax({
    url      : url,
    dataType : \'json\', // I          


        
4条回答
  •  一生所求
    2020-11-22 11:00

    No, the dataType option is for parsing the received data.

    To post JSON, you will need to stringify it yourself via JSON.stringify and set the processData option to false.

    $.ajax({
        url: url,
        type: "POST",
        data: JSON.stringify(data),
        processData: false,
        contentType: "application/json; charset=UTF-8",
        complete: callback
    });
    

    Note that not all browsers support the JSON object, and although jQuery has .parseJSON, it has no stringifier included; you'll need another polyfill library.

提交回复
热议问题