PDA

View Full Version : [SOLVED] a question about submitting



pirusira
27 Jun 2010, 10:20 PM
Hello all.

I've been making a form to submit and insert data.
The following is a part of the submitting in js file.


addUserFormOb.submit({
method: 'POST',
waitTitle: 'Connecting',
waitMsg: 'Sending data...',
success: function () {
userListGrid.load();
},
failure: function () {
alert('failed');
addUserFormOb.reset();
}
});


And this is a part of the java code for processing the data.


try {
sqlMapper.insert("addUser", addParams);
} catch (Exception e) {
e.printStackTrace();
success = false;
}
if (success) {
return "success";
}
else return "error";


Those are working pretty well, but ExtJS doesn't receive the success value.
So the sentence "alert('failed');" is excuted.
How do I pass the 'success' value to ExtJS?

Thanks in advance.
Stan

Condor
27 Jun 2010, 10:46 PM
What kind of response do the "success" and "error" actions generate?

For a form submit you should be returning:

{"success":true}

pirusira
27 Jun 2010, 11:51 PM
Thank you for the answer, Condor.

But, I'm using struts2, so it has to return a String value like "success" or "error" or something.
How do I return a String type value to struts2 as a result of the action and an array like {"success":true} to ExtJS at the same time?

Animal
28 Jun 2010, 12:00 AM
"{\"success\":true}" is a string.

Condor
28 Jun 2010, 12:16 AM
I'm not that familiar with Struts, but "success" and "error" are not strings that are returned to the client. They are the names of forward specifications in the config file, e.g.

<action ...>
...
<forward name="success" path="success.jsp" />
<forward name="error" path="error.jsp"/>
</action>
In this example "success.jsp" needs to set the Content-type to application/json and return {"success":true} (and no HTML markup!)

pirusira
28 Jun 2010, 12:23 AM
Oh, I solved.

I coded succes.jsp file like this.


<%@ page language="java" contentType="application/json; charset=UTF-8"
pageEncoding="UTF-8"%>

<%
out.print("{success:true}");
%>


Thank you very much.

Animal
28 Jun 2010, 1:06 AM
You will eventually run into problems if you write like that though.

Use the json classes from http://json.org/ to generate JSON if you need anything more complex than that.

It handles character escaping for you and generates standards-compliant JSON which mat be parsed by browser native JSON parsing (That's optional, but it's good to adhere to standards)