1,Content-Type:application/x-www-form-urlencoded
使用Ajax默認格式來傳遞數據【推薦】 Ajax的默認格式為:application/x-www-form-urlencoded, 相當於(username=“admin”&password=123) 來傳遞數據(這是GET請求的固定格式) 前端代碼: 當Ajax以默認格式上傳時,data數據直接使用JSON對象user, 不用轉換為JSON字符串(很方便) var user= { "username" : username, "password" : password, "rememberMe":rememberMe }; $.ajax({ url : "http://...../jsontest.do", type : "POST", async : true, data : user, dataType : 'json', success : function(data) { } });
2,Content-Type使用application/json
Content-Type使用application/json的時候, 要將JSON對象轉換為JSON字符串 前端代碼: 這里 data : JSON.stringify(user), var user= { "username" : username, "password" : password }; $.ajax({ url : "http://...../jsontest.do", type : "POST", async : true, contentType: "application/json; charset=utf-8", data : JSON.stringify(user), dataType : 'json', success : function(data) { } });