jquery ajax form serialize() 转json 数据,post 后台

$(".submit").click(function (){
$.ajaxSetup({
   contentType: "application/x-www-form-urlencoded; charset=utf-8"
});
var DataDeal = {
          formToJson: function (data) {
              data=data.replace(/&/g,"\",\"");
              data=data.replace(/=/g,"\":\"");
              data="{\""+data+"\"}";
              return data;
           },
};
var  userId = $('.userId').val();
var data =$("#addUserAddressJson").serialize();//获取值
data= decodeURIComponent(data,true);//防止中文乱码
var json=DataDeal.formToJson(data);//转化为json
var  url = "${ctx}/address/addUserAddressJson";
$.ajax({
type : "POST",
url : url,
dataType : "text",
contentType : "application/json",
data :json,
success : function(data) {
if(data== "fail"){
              window.location.href = "${ctx}/login?url=" + data;
            }
if (data== "success") {
window.location.href = "${ctx}/address/getAddressList?userId="+userId;
}
},
   error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.status);
                alert(XMLHttpRequest.readyState);
                alert(textStatus);
            }
});


});

你可能感兴趣的:(post,后台,jquery,ajax,form,serialize(),转json,数据)