(一)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;
}
相关推荐
在本场景中,我们将探讨如何使用AJAX来传递JSON数组,并在Struts2的Action中接收和处理这些数据。 首先,JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,它易于人阅读和编写,同时也易于机器解析...
JSON(JavaScript Object Notation)作为一种轻量级的数据交换格式,因其易读性、易写性和高效性,成为了Ajax通信中数据传输的首选格式。本篇文章将深入探讨在Ajax中如何使用JSON来传输数据,以及涉及到的相关工具和...
在 AJAX 中,我们通常使用 JSON 格式作为数据传输的载体,因为它比 XML 更快、更简洁。 AjaxJson 实例通常包括以下步骤: 1. **创建 XMLHttpRequest 对象**:这是 AJAX 的基础,用于与服务器进行通信。在 ...
Ajax(Asynchronous JavaScript and XML)技术允许我们在不刷新整个页面的情况下,实现局部数据的更新,而Json(JavaScript Object Notation)则是一种轻量级的数据交换格式,易于人阅读和编写,同时也方便机器解析...
在现代Web开发中,前后端的数据交互是必不可少的,而Ajax和Beego框架为我们提供了高效且灵活的方式来进行这种交互,特别是在处理JSON格式的数据时。本文将深入探讨如何使用Ajax与Beego实现前后端间的JSON数据交换。 ...
在介绍aspx后台传递Json到前台的两种接收方法之前,我们先了解一些基础知识点。Json(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成,现在已经成为一种广泛...
Json数据格式通常用于Ajax请求中,作为传输的数据载体,因为它能被JavaScript直接解析,无需额外的转换步骤。 在本视频教程的第一讲中,你可能会学到以下知识点: 1. Ajax的基础概念:理解什么是Ajax,它的基本...
总结来说,Ajax向Spring MVC传递JSON涉及的主要步骤包括前端的数据序列化、Ajax请求的发起、服务器端的参数绑定、业务处理及响应的JSON化。理解并熟练掌握这一流程对于进行高效的前后端数据交互至关重要。在实际项目...
JSON通常用于在服务器和客户端之间传递数据,其中时间戳通常是字符串或数字格式,而我们往往希望在前端将其转化为用户友好的日期和时间格式。 在描述中提到的"js全部都封装好了",这可能指的是`ajaxDataFormat.js`...
SSH 框架 Ajax 获取的 JSON 格式数据处理过程 在本文中,我们将详细讲解如何使用 SSH 框架中的 Ajax 获取的 JSON 格式数据处理过程,并且实现菜单联动下拉框的功能。 一、Ajax 中 response 返回的数据是一个二维...
综上所述,Struts2和Ajax的结合使用,能够实现高效的前后端数据交互,通过JSON数据格式,使数据传输更加便捷和高效。在实际项目中,开发者应熟练掌握这两种技术的结合,以提高Web应用的用户体验。
在IT领域,文件上传和AJAX获取JSON是两个常见的功能,尤其在Web开发中。这里我们探讨的是一个基于JSP的文件上传示例,以及如何通过AJAX技术实现实时刷新,从数据库获取并返回JSON数据到前端页面。 首先,文件上传是...
在Ajax请求中,通常JSON被用作数据传输格式,因为它可以直接被JavaScript解析为对象,无需额外的序列化和反序列化过程。 **Spring MVC、Ajax和JSON的结合** 在Spring MVC中,Controller可以返回JSON格式的数据响应...
PHP 基于 jQuery 的 Ajax 技术传递 JSON 数据简单实例 本文主要介绍了 PHP 基于 jQuery 的 Ajax 技术传递 JSON 数据方法,以完整实例形式分析了 PHP 基于 jQuery 的 Ajax 无刷新提交数据实现方法。下面是详细的知识...
文件名`ajax_json_curl`暗示了这是一个关于AJAX、JSON和cURL的组合实现,可能包含了示例代码、配置文件或测试数据。 总结起来,"php-curl+ajax+json数据传输2.0版本"是利用PHP-cURL解决AJAX跨域请求的一种解决方案...
本篇文章主要介绍了jQuery Ajax前后端使用JSON进行交互示例,实现前端通过jQuery Ajax传输json到后端,后端接收json,对json进行处理,后端返回一个json给前端,有兴趣的可以了解一下。
在前端开发中,Ajax(Asynchronous JavaScript and XML)是一种创建动态网页的技术,允许在不刷新整个页面的情况下与服务器交换数据并更新部分...同时,熟悉JSON和Ajax的工作原理,对于避免和解决这类问题至关重要。
1. **数据传输**:AJAX发送请求时,请求体通常携带JSON格式的数据,服务器接收到后,可以方便地反序列化为对象处理。 2. **响应数据**:服务器处理完请求后,将结果封装为JSON格式,通过HTTP响应返回给客户端。 3. *...
在Ajax中,JSON常用来作为数据传输的格式,因为它可以直接被JavaScript解析为对象。 3. **PHP处理数据**:PHP服务器端脚本可以接收用户请求,处理业务逻辑,如从数据库查询数据,进行计算等,然后将结果封装成JSON...