java web 传值_前端jsp与后端servlet传值(java web)

jsp中js使用jQuery封装好的 $.ajax()方法与服务器进行交互

var jsonStick = this.strtoJson();

$.ajax({

type : 'post',//不起作用 要用jsonP

url : 'controllerServlet',//要传输数据对象的地址

contentType : "application/x-www-form-urlencoded",//转化为form表单传值

data : {

"js" : jsonStick

}, //前面的js是服务端获取的ID 后面的jsonStick是处理后的json对象

cache : false,

dataType : 'text',//这个关系到你能不能正确接收到servlet 响应的数据

async : false, //因为ajax默认是异步调用的,所以得到的返回值是空值,要得到值必须改成同步:async: false

success : function(data) {

//console.log(data);

flag = data; //采用全局变量flag接收数据(data未作处理)

},

error : function(msg) { //ajax请求失败后触发的方法

//弹出错误信息

console.log(msg);

}

});

//数据处理函数

function() {

//'':this.note.name,

var arr = [];

var json = {};

json.note_id = this.id

json.note = this.text;

json.left = this.left;

json.top = this.top;

json.zindex = this.zIndex;

if (this.sqlId == null) {

this.sqlId = 0;

}

json.sqlId = this.sqlId;

arr.push(json);

var jsonStick = JSON.stringify(arr);//格式化json数据

return jsonStick;

}

servlet接收 处理 响应

protected void doGet(.........){

String js = request.getParameter("js");//接收jsp传过来的数据

JSONArray json = JSONArray.fromObject(js);//对数据进行数组转化

JSONObject jsonOne = json.getJSONObject(0);//获取确定的json对象

boolean flag = true;

PrintWriter out = response.getWriter();

out.print(flag);//将flag传给jsp

out.close();

}

jsp接收响应 并对数据进行处理

$.ajax({

type : 'post',

url : 'sendServlet',

cache : false,

dataType : 'json',

async : false, //因为ajax默认是异步调用的,所以得到的返回值是空值,要得到值必须改成同步:async: false

success : function(data) {

//console.log(data.result.note)

for (var i in data) {

console.log(data[i])

note = new Note();

note.id = data[i].note_id;

note.text = data[i].note;

note.timestamp = new Date().getTime();

note.left = data[i].left;

note.top = data[i].top;

note.zIndex = data[i].zindex;

}

},

error : function(msg) { //ajax请求失败后触发的方法

//弹出错误信息

console.log(msg);

}

});

中间用到的jar包以及js

https://pan.baidu.com/s/1XEOkM1as_10I92WAzooBuA

密码:gix8

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值