用原生的AJax代码发送JSON数据到Action使其自动类型转化

来源:互联网 发布:python获取字符串长度 编辑:程序博客网 时间:2024/06/05 03:03

擦写完这个博客我就睡觉弄了一晚上终于懂了怎样用用原生的AJax代码发送JSON数据到Action使其自动类型转化了

1.前台

  自己写了一个函数request用于发送Ajax请求传入一个参数Object 看源码

var request = function(obj) {// setting extendvar url = obj.url || "";var method = obj.method || "GET";var data = obj.data || null;var callback = obj.callback || undefined;var json = obj.json || false;var contentType = obj.contentType|| "application/x-www-form-urlencoded";var xhr = new XMLHttpRequest();xhr.open(method, url, true);// 设置请求数据的类型xhr.setRequestHeader("Content-Type", contentType);// 设置返回执行的函数xhr.onreadystatechange = function() {if (xhr.readyState == 4 && xhr.status == 200) {callback ? callback(xhr.responseText) : "";}}json ? xhr.send(JSON.stringify(data)) : xhr.send(obj2query(data));}

2后台的Action

package com.zealgrown.web.action;import java.io.ByteArrayInputStream;import java.io.InputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;public class GetSubCategoriesAction extends BaseAction {private InputStream inputStream;// 种类的idprivate int categoryId;// 页数和每页的个数private int pageNo;private int pageSize;@Overridepublic String execute() throws Exception {//String pageSizea = ServletActionContext.getRequest().getParameter(//"pageSize");//String pageNoa = ServletActionContext.getRequest().getParameter(//"pageNo");String subCategories = "";if (categoryId > 0) {if (pageNo > 0 && pageSize > 0) {subCategories += subCategoryService.getSubCategoryByPageAndParent(pageNo, pageSize,categoryId);} else {subCategories += subCategoryService.getSubCategoryByPageAndParent(1, 10, categoryId);}} else {// 如果传来的categoryId不是有效的数字是字符串if (pageNo > 0 && pageSize > 0) {subCategories += subCategoryService.getSubCategoryByPage(pageNo, pageSize);} else {subCategories += subCategoryService.getSubCategoryByPage(1, 10);}}inputStream = new ByteArrayInputStream(subCategories.getBytes("UTF-8"));return SUCCESS;}public InputStream getInputStream() {return inputStream;}public void setInputStream(InputStream inputStream) {this.inputStream = inputStream;}public int getCategoryId() {return categoryId;}public void setCategoryId(int categoryId) {this.categoryId = categoryId;}public int getPageNo() {return pageNo;}public void setPageNo(int pageNo) {this.pageNo = pageNo;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}}


其中obj2query函数:

var obj2query = function(obj) {var query = "";for ( var item in obj) {if (obj.hasOwnProperty(item)) {// console.log("item is " + item + " and obj.item is " + obj[item]);query += item + "=" + obj[item] + "&";}}return query.substring(0, query.length - 1);};


现在想对 一个action上交数据:

有两种方法:

Get方法:

request({url : baseUrl + "getSubCategories?categoryId=" + categoryId+ "&pageNo=2&pageSize=3,callback : randerSubCategories});

Post方法:

request({url:baseUrl + "getSubCategories", data : { categoryId : id, pageNo : "1", pageSize : "2" }, method : "post",callback : randerSubCategories});

注意事项:

    1.如果后台的类型为int前台传入的参数一定要是数字的字符串而且不能为null和undefined要不后台会报这个警告:

   警告: Could not find action or result: /ZealGrown/getSubCategories
No result defined for action com.zealgrown.web.action.GetSubCategoriesAction and result input - action - file:/D:/Program%20Files/apache-tomcat-8.0.27/webapps/ZealGrown/WEB-INF/classes/struts-user.xml:25:66

虽然我们已经在配置文件中配置了/getSubCategories

    2.方法是post的时候要用obj2query转化成pageNo=2&pageSize=2&categoryId=4的形状,因为Ajax的send发送时会将其变成请求对。

   不能用JSON字符串,因为ajax不会解析JSON字符串Ajax会直接发送JSON字符串给后台,但是struct2不会解析JSON字符串,即SpringBoot 和 Spring MVC到不能直接解析JSON字符串。

     用chrome查看发送的数据包的时候必须如下图


3.不能将Content-Type 设置成application/json形式这样action获取不了参数的如下图:


不过.net的mvc可以这样的详细请看:点击打开链接


擦擦1:12了睡觉擦擦擦

0 0
原创粉丝点击