ajax(第一篇)将json数据提交到action并解析
2016-11-01 17:13
344 查看
<%@ 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;
}
}
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;
}
}
相关文章推荐
- ajax提交json数据到后端C#解析
- struts2 + ajax(由前台的form提交数据到后台,再根据form所调用返回获取的后台json格式的数据返回到前端,然后前端用jquery对json数据进行解析)==》》涉及非文件上传的部分
- ajax 提交post get 数据,得到json结果解析
- struts2 + ajax(由前台的form提交数据到后台,再根据form所调用返回获取的后台json格式的数据返回到前端,然后前端用jquery对json数据进行解析)==》》涉及文件上传的部分
- ajax提交json数据,后台解析问题
- ajax提交json数据,后台解析问题
- IE8下struts2 采用ajax提交表单,返回json字符串,解析不了,直接保存json字符串。提示下载action.
- json_ajax提交json格式数据到servlet并解析
- easyui使用Ajax提交表单,返回Json数据
- AJAX提交到Handler.ashx一般处理程序返回json数据
- ajaxfileupload以JSON为返回数据类型出现的因符号无法解析的错误的解决方案
- jquery发送ajax请求,以及struts2的action响应请求并传回json数据的做法
- MVC中使用Ajax提交数据 Jquery Ajax方法传值到action
- ajax 框架 dojo 提交json数据乱码的解决方法
- QML中使用AJAX,解析返回的JSON数据
- MVC中使用Ajax提交数据 Jquery Ajax方法传值到action
- client提交json(nodejs)和server解析json并返回json数据(express)
- jQuery学习笔记--ajax查询,jQuery解析返回的json数据 详解
- 教你用jQuery 和 Ajax 解析JSON数据。读取XHR妥妥的