ajax(第一篇)将json数据提交到action并解析

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ajax(第一篇)将json数据提交到action并解析相关的知识,希望对你有一定的参考价值。

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

<script type="text/javascript" src="json2.js"></script>

<script type="text/javascript">

var myAjaxObject;

function AjaxTransferText() {

var BigText = document.getElementById("BigText").value;

var AjaxTransferObjectRef = new AjaxTransferObject("username", "password",

10, BigText);

var JSONString = JSON.stringify(AjaxTransferObjectRef);

if(window.ActiveXObject){

myAjaxObject = new ActiveXObject("Microsoft.XMLHTTP");

}else {

myAjaxObject = new XMLHttpRequest();

}

var urlString = "jsonString=" + JSONString;

alert(urlString);

myAjaxObject.open("POST", "getJSON.action"+"?timestamp" + new 

Date().getTime(),true);

myAjaxObject.setRequestHeader("Content-Type",

 "application/x-www-form-urlencoded")

myAjaxObject.send(urlString);

}

function AjaxTransferObject(username, password, age, BigText){

this.username = username;

this.password = password;

this.age = age;

this.BigText = BigText;

}

function AjaxTransferTextByget(){

var BigText = document.getElementById("BigText").value;

var AjaxTransferObjectRef = new AjaxTransferObject("username", "password",

10, BigText);

var JSONString = JSON.stringify(AjaxTransferObjectRef);

if(window.ActiveXObject){

myAjaxObject = new ActiveXObject("Microsoft.XMLHTTP");

}else {

myAjaxObject = new XMLHttpRequest();

}

var urlString = "jsonString=" + JSONString;

alert(urlString);

myAjaxObject.open("POST", "getJSON.action"+urlString,true);

myAjaxObject.setRequestHeader("Content-Type",

 "application/x-www-form-urlencoded")

}

</script>

</head>

<body>

<textarea rows="5" cols="45" id="BigText"></textarea>

<br/>

<input type="button" value="test" onclick="AjaxTransferText()">

<input type="button" value="test" onclick="AjaxTransferTextByget()">

</body>

</html>


Action代码如下:

package controller;


import com.opensymphony.xwork2.ActionSupport;


import net.sf.json.JSONObject;


public class GetJSON extends ActionSupport{

private String jsonString;

public String getJsonString(){

return this.jsonString;

}

public void setJsonString(String jsonString){

this.jsonString = jsonString;

}

@Override

public String execute() throws Exception {

System.out.println(jsonString);

JSONObject json = JSONObject.fromObject(jsonString);

System.out.println("username=" + json.get("username"));

System.out.println("password=" + json.get("password"));

System.out.println("age=" + json.get("age"));

System.out.println("BigText=" + json.get("BigText"));

return null;

}

}


本文出自 “五小猪” 博客,请务必保留此出处http://wuxiaozhu.blog.51cto.com/7942143/1868177

以上是关于ajax(第一篇)将json数据提交到action并解析的主要内容,如果未能解决你的问题,请参考以下文章

前台怎么接受后台的数据

通过 JSON 将数据从 handsontable 检索到 Struts2 Action 不起作用

在JAX调用中将Json数据发送到@ Html.Action

通过button将form表单的数据提交到action层

jQuery实现ajax提交form表单(可以是提交json),用springmvc接收。图文详解

ajax 返回的是json 怎么获取到