html 接收json数据格式,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\":\"\",\"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\":\"\",\"applyAmount\":\"\"}," +

"\"careerInfo\":{\"jobName\":\"\",\"reserveFundsRadix\":\"\",\"salaryDate\":\"\",\"companyCity\":\"\",\"companyType\":\"\",\"hasSocialSecurity\":\"\",\"industry\":\"\",\"jobLevel\":\"\",\"industryForth\":\"\",\"salaryWithoutTax\":\"\",\"employeeType\":\"\",\"companyPhone\":\"\",\"industrySecond\":\"\",\"companyAddress\":\"\",\"industryThird\":\"\",\"company\":\"\",\"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":"","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":"","applyAmount":""},

"careerInfo":{"jobName":"","reserveFundsRadix":"","salaryDate":"","companyCity":"","companyType":"","hasSocialSecurity":"","industry":"","jobLevel":"","industryForth":"","salaryWithoutTax":"","employeeType":"","companyPhone":"","industrySecond":"","companyAddress":"","industryThird":"","company":"","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) // 图片数据流

},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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值