struts2 ajax 错误,jquery - Ajax error result with struts2 - Stack Overflow

这篇博客介绍了如何在JSP网站上使用JSON.simple库来编码和解码JSON消息。示例展示了服务器端的JSP编码服务以及客户端的XMLHttpRequest请求,通过AJAX获取并解析JSON数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hello Ging3r,

Normally, JSP websites require a JSON plugin to encode/decode JSON messages.

JSON.simple is a good example of a JSON plugin. The JSON.simple website also provides tutorials for setting up JSON on JSP websites.

If you prefer using plugins specific to Struts, there are a couple of JSON plugins for Struts2 on Apaches website.

Edit

I’m absolutely not an expert on JSP, and know nothing about the JSON Struts plugin you are using, although I know a little bit about json-simple.

Nevertheless, the following two examples make sense for me. The examples are from the tutorial mentioned above.

Example 1 - Server side JSP encoding

service.jsp:

JSONObject obj=new JSONObject();

obj.put("name","foo");

obj.put("num",new Integer(100));

obj.put("balance",new Double(1000.21));

obj.put("is_vip",new Boolean(true));

obj.put("nickname",null);

out.print(obj);

out.flush();

%>

Example 2 - Client side XMLHttpRequest

client.html:

function createXMLHttpRequest(){

// See http://en.wikipedia.org/wiki/XMLHttpRequest

// Provide the XMLHttpRequest class for IE 5.x-6.x:

if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function() {

try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}

try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}

try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}

try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}

throw new Error( "This browser does not support XMLHttpRequest." )

};

return new XMLHttpRequest();

}

var AJAX = createXMLHttpRequest();

function handler() {

if(AJAX.readyState == 4 && AJAX.status == 200) {

var json = eval('(' + AJAX.responseText +')');

alert('Success. Result: name => ' + json.name + ',' + 'balance => ' + json.balance);

}else if (AJAX.readyState == 4 && AJAX.status != 200) {

alert('Something went wrong...');

}

}

function show(){

AJAX.onreadystatechange = handler;

AJAX.open("GET", "service.jsp");

AJAX.send("");

};

Click here to get JSON data from the server side

You can find more information about these examples here.

PS!

You should also look into the solution suggested by Dave Newton. He is far more experienced than me, and knows what he is talking about.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值