jquery Asynchronous request action returns JSON

Source: Internet
Author: User

Reprint Please specify the Source: Jiq ' s technical Blog

Recently, in the Web management interface of the distributed registry, you need to click on the left tree node, request background action to query the node's detailed data, and then asynchronously flush the right node information area.

Asynchronous refreshes are packaged with jquery for Ajax:

function Ztreeonclick (event, Treeid, TreeNode) {        $.ajax ({            type: "POST",            URL: "Config.action",            Data: "") ,            dataType: "JSON",            success:function (data) {                        alert (eval ("(" +data.result+ ")"). Word);                $ ("#configInfo"). Load ("nodeinfo.jsp");            $ ("Div. Configinfo"). HTML (responsetext);            },            error:function () {            $ ("#configInfo"). Load ("error.jsp" );                        }        });     };

action is written like this:

@SuppressWarnings ("Serial") public class Configmanageaction extends Actionsupport{private String result;@ Overridepublic String Execute () throws exception{map<string,object> Map = new hashmap<string,object> ();        Map.put ("word", "word_a");        Map.put ("WordCount");             Jsonobject JSON = jsonobject.fromobject (map);        This.result = Json.tostring ();//Assign value to result, pass to page return SUCCESS;} Public String GetResult () {return result;} public void Setresult (String result) {This.result = result;}}

struts.xml Configuration:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "http://struts.apache.org/dtds/ Struts-2.3.dtd "><struts> <constant name=" Struts.enable.DynamicMethodInvocation "value=" false "/> &lt ; constant Name= "Struts.devmode" value= "true"/> <package name= "Default" namespace= "/" extends= "Struts-default, Json-default "> <action name=" Login "class=" org.zk.action.LoginAction "> <result name=" Success                ">/index.jsp</result> <result name=" error ">/error.jsp</result> </action> <action name= "test" class= "Org.zk.action.ConfigManageAction" method= "method"/> <action name= "Co Nfig "class=" org.zk.action.ConfigManageAction "> <result name=" Success "type=" JSON "/> </acti On> </package></struts>

Note: There is also a way to configure the action method that does not return any values, the corresponding action configuration does not configure any return type, but although the action method does not return a value, it needs to write the return value into the response stream, like this:

Public String Method () throws Exception{httpservletresponse response = Servletactioncontext.getresponse (); Response.setcontenttype ("Application/json;charset=utf-8"); Response.setcharacterencoding ("UTF-8"); Response.setheader ("Cache-control", "No-cache"); PrintWriter pw  = Response.getwriter (); map<string,object> map = new hashmap<string,object> ();        Map.put ("word", "word_a");        Map.put ("WordCount");             Jsonobject JSON = jsonobject.fromobject (map),//Convert the Map object to JSON type data        pw.write (json.tostring ());p W.flush (); Pw.close (); return null;}

no matter how I try, this jquery asynchronous request always returns error!


You have to check the results returned by the page request, open chrome and go to the admin interface and press F12 to debug the interface.

Click on the tree node, in the Network tab page to see all requests and corresponding responses, found that although the Config.action request return code is 200, that is normal, but response is all abnormal!!!

Then the exceptions are eliminated, basically all because of the small jar package, and the last Jar package is as follows:



This returns success, but parsing the returned JSON string is always an error, and my return is as follows:



And then the web to parse the JSON should be: {' key1 ': ' value1 '}, and with a shape such as:

var obj = eval ("(" + Strjson + ")");//The converted JSON Object alert (obj.name);//json name

this way to parse, a look at my return JSON, found a more result, so in this way parse on the line:

Alert (eval ("+data.result+"). Word);

jquery Asynchronous request action returns JSON

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.