ajax的接收格式和传输json格式种类和方式

(一)ajax返回格式

1,默认返回字符串,页面也会转成字符串(text)

参考:

http://www.cnblogs.com/bujianchenxi/p/6060799.html

2返回页面

$.ajax({

                    type:'post',

                    url:'后台路径',

                    dataType:'html',

                    success:function(result){

                    //console.info(result);

                    $("#test").html(result);

                }

            });

注意:dataType:'html'为html类型,后台实现,如下:

@RequestMapping(value = "test")

    public String test(Model model) {

        model.addAttribute("test", "test");

        return "modules/test/test";

    }

return一个页面,例如上面的对应的视图为test.jsp

参考:

http://blog.csdn.net/menghuannvxia/article/details/49664059

http://blog.csdn.net/liu765023051/article/details/45308827

 3,ajax  json  前台接收解析

  var dataJsonObject = JSON.parse(data);

   if (dataJsonObject.returnCode == 1 ) {

(二)ajax传输json格式:

ajax   json 前台打包传输

遇到“”里面套用“”需要转义

1,使用转义符,此时不需要JSON.stringify()后传递过去(整个不用引号引起)

var jsonsWxInFo="{\"result\":{" +

  "\"baseInfo\":{\"liveAddress\":\"\",\"tenementCity\":\"\",\"homePhone\":\"\",\"tenementAddress\":\"\",\"qqAccount\":\"\",\"hasTenement\":\"\",\"liveCity\":\"\",\"monthSalary\":\"\",\"educationLevel\":\"\",\"marriage\":\"\",\"id\":\"\",\"weixinAccount\":\"\",\"tenementType\":\"\",\"email\":\"\",\"hasCar\":\"\",\"liveStarttime\":\"\"}," +

  "\"contactInfo\":{\"immediateFamilyIdcard\":\"\",\"postAddress\":\"\",\"spousePhone\":\"\",\"contactPerson\":\"\",\"contactPersonPhone\":\"\",\"getProductWay\":\"\",\"immediateFamilyPhone\":\"\",\"contactPersonRelation\":\"\",\"spouse\":\"\",\"nonImmediateFamilyName\":\"\",\"nonImmediateFamilyRelation\":\"\",\"immediateFamilyName\":\"\",\"spouseIdcard\":\"\",\"nonImmediateFamilyPhone\":\"\",\"id\":\"\",\"immediateFamilyRelation\":\"\",\"isFamilyKnow\":\"\",\"marryDate\":\"\"}," +

  "\"policyInfo\":{\"actualYear\":\"\",\"policyName\":\"\",\"inputUserid\":\"\",\"shouldYear\":\"\",\"inputDate\":\"\",\"paymentType\":\"\",\"lifeInsuranceYearSum\":\"\",\"inputOrgId\":\"\",\"monthAmt\":\"\",\"useDate\":\"\",\"policyCount\":\"\",\"policyType\":\"\",\"customerId\":\"\",\"insuranceName\":\"\",\"policyIndex\":\"\"}," +

  "\"additionalInfo\":{\"otherIdentityPicUrl\":\"\",\"otherPicUrl\":\"\",\"loanPicUrls\":\"\",\"juzhuzhengPicUrls\":\"\",\"creditCardPicUrls\":\"\",\"hukoubenPicUrls\":\"\",\"gongzuozhengPicUrls\":\"\",\"identityFrontPicUrl\":\"bluckname=test&imgName=MTExLnBuZzE1MDc3MDkzNTkxNzI=.png\",\"identityBackPicUrl\":\"\",\"marriageCertificatePicUrls\":\"\",\"zichanLetterPicUrls\":\"\",\"id\":\"4\",\"shouruLetterPicUrls\":\"\"}," +

  "\"additionalInfoRemarks\":{\"dealUserid\":\"\",\"createTime\":\"\",\"dealUsername\":\"\",\"remark\":\"\",\"id\":\"\"}," +

  "\"applyInfo\":{\"loanTerm\":\"\",\"purpose\":\"\",\"id\":\"\",\"applyAmount\":\"\"}," +

  "\"careerInfo\":{\"jobName\":\"\",\"reserveFundsRadix\":\"\",\"salaryDate\":\"\",\"companyCity\":\"\",\"companyType\":\"\",\"hasSocialSecurity\":\"\",\"industry\":\"\",\"jobLevel\":\"\",\"industryForth\":\"\",\"salaryWithoutTax\":\"\",\"employeeType\":\"\",\"companyPhone\":\"\",\"industrySecond\":\"\",\"companyAddress\":\"\",\"industryThird\":\"\",\"company\":\"\",\"id\":\"\",\"department\":\"\",\"socialSecurityRadix\":\"\",\"employeeAmount\":\"\"}," +

  "\"creditInfo\":{\"bankPicUrls\":\"\",\"peopleBankPicUrls\":\"\",\"reserveFundsPicUrls\":\"\",\"id\":\"\"}}," +

  "\"orderNo\":\"A001\",\"state\":\"\",\"isEiditAble\":\"1\"}";

  function saveDetailWX(){

  alert(6);

  $.ajax({

  url: '${ctx}/itm/weixin/saveWXOrderInfo.action',

  data: {

  jsonsWxInFo: jsonsWxInFo // 图片数据流

  },

  dataType: 'json',

  type: 'post',

  success: function(data) {

  var dataJsonObject = JSON.parse(data);

  if (dataJsonObject.returnCode == 'A0001' ) {

  }else{

  alert('图片上传失败!');

  }

  },

  error: function(xhr, type, errorThrown) {

  alert('网络异常,请稍后再试!');

  }

  });

  }

2,不使用转义符,此时用JSON.stringify()将数据传递过去

 var jsonsWxInFo={"result":{

  "baseInfo":{"liveAddress":"","tenementCity":"","homePhone":"","tenementAddress":"","qqAccount":"","hasTenement":"","liveCity":"","monthSalary":"","educationLevel":"","marriage":"","id":"","weixinAccount":"","tenementType":"","email":"","hasCar":"","liveStarttime":""},

  "contactInfo":{"immediateFamilyIdcard":"","postAddress":"","spousePhone":"","contactPerson":"","contactPersonPhone":"","getProductWay":"","immediateFamilyPhone":"","contactPersonRelation":"","spouse":"","nonImmediateFamilyName":"","nonImmediateFamilyRelation":"","immediateFamilyName":"","spouseIdcard":"","nonImmediateFamilyPhone":"","id":"","immediateFamilyRelation":"","isFamilyKnow":"","marryDate":""},

  "policyInfo":{"actualYear":"","policyName":"","inputUserid":"","shouldYear":"","inputDate":"","paymentType":"","lifeInsuranceYearSum":"","inputOrgId":"","monthAmt":"","useDate":"","policyCount":"","policyType":"","customerId":"","insuranceName":"","policyIndex":""},

  "additionalInfo":{"otherIdentityPicUrl":"","otherPicUrl":"","loanPicUrls":"","juzhuzhengPicUrls":"","creditCardPicUrls":"","hukoubenPicUrls":"","gongzuozhengPicUrls":"","identityFrontPicUrl":"bluckname=test&imgName=MTExLnBuZzE1MDc3MDkzNTkxNzI=.png","identityBackPicUrl":"","marriageCertificatePicUrls":"","zichanLetterPicUrls":"","id":"4","shouruLetterPicUrls":""},

  "additionalInfoRemarks":{"dealUserid":"","createTime":"","dealUsername":"","remark":"","id":""},

  "applyInfo":{"loanTerm":"","purpose":"","id":"","applyAmount":""},

  "careerInfo":{"jobName":"","reserveFundsRadix":"","salaryDate":"","companyCity":"","companyType":"","hasSocialSecurity":"","industry":"","jobLevel":"","industryForth":"","salaryWithoutTax":"","employeeType":"","companyPhone":"","industrySecond":"","companyAddress":"","industryThird":"","company":"","id":"","department":"","socialSecurityRadix":"","employeeAmount":""},

  "creditInfo":{"bankPicUrls":"","peopleBankPicUrls":"","reserveFundsPicUrls":"","id":""}},

  "orderNo":"A001","state":"","isEiditAble":"1"};

  function saveDetailWX(){

  alert(6);

  $.ajax({

  url: '${ctx}/itm/weixin/saveWXOrderInfo.action',

  data: {

  jsonsWxInFo: JSON.stringify(jsonsWxInFo) // 图片数据流

  },

  dataType: 'json',

  type: 'post',

  success: function(data) {

  var dataJsonObject = JSON.parse(data);

  if (dataJsonObject.returnCode == 'A0001' ) {

  }else{

  alert('图片上传失败!');

  }

  },

  error: function(xhr, type, errorThrown) {

  alert('网络异常,请稍后再试!');

  }

  });

  }

后台json结构数据和实体转化

public String saveWXOrderInfo(){

QueryOrderDetailReqqp=(QueryOrderDetailReq)JSONObject.toBean(JSONObject.fromObject(jsonsWxInFo),QueryOrderDetailReq.class);

//baseService.save();

BaseResp br = new BaseResp();

br.setReturnCode(WeixinErrorMsg.SUCCESS.getCode());

br.setReturnMsg(WeixinErrorMsg.SUCCESS.getText());

result= JSONObject.fromObject(br).toString();

        return SUCCESS;

}

猜你喜欢

转载自yuhuiblog6338999322098842.iteye.com/blog/2396021
今日推荐