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

边银龙
2023-12-01

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.

 类似资料: