How can I send JSON data to server

前端 未结 7 998
旧巷少年郎
旧巷少年郎 2021-01-04 00:25

Well, here is the story:

I have some data need to send to server, but they should turned into JSON dataType first.

I made such ajax call:

            


        
7条回答
  •  执念已碎
    2021-01-04 01:02

     var data = {'bob':'foo','paul':'dog'};
     $.ajax({
       url: url,
       type: 'POST',
       contentType:'application/json',
       data: JSON.stringify(data),
       dataType:'json'
     });
    

    /** Added **/

    The above does not do anything with the response from the server if you need to do something then a callback will be called when the server has responded.

     var data = {'bob':'foo','paul':'dog'};
     $.ajax({
       url: url,
       type: 'POST',
       contentType:'application/json',
       data: JSON.stringify(data),
       dataType:'json',
       success: function(data){
         //On ajax success do this
         alert(data);
          },
       error: function(xhr, ajaxOptions, thrownError) {
          //On error do this
            if (xhr.status == 200) {
    
                alert(ajaxOptions);
            }
            else {
                alert(xhr.status);
                alert(thrownError);
            }
        }
     });
    

提交回复
热议问题