ajax的接收格式和传输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;

}

原文链接:https://www.f2er.com/ajax/160685.html

猜你在找的Ajax相关文章