Jquery asynchronous request Action returns JSON

Source: Internet
Author: User

Jquery asynchronous request Action returns JSON

Reprinted please indicate the source:Jiq? Chin's technical Blog

Recently, I wrote the Web management interface of the distributed registration center. When I need to click the tree node on the left, I requested the background Action to query the detailed data of the node, and then asynchronously refreshed the node information area on the right.

Asynchronous refresh uses Jquery's Ajax encapsulation:

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");                        }        });     };

The Action is written as follows:

@ SuppressWarnings ("serial") public class ConfigManageAction extends ActionSupport {private String result; @ Overridepublic String execute () throws Exception {Map
 
  
Map = new HashMap
  
   
(); Map. put ("word", "word_A"); map. put ("wordcount", 33); JSONObject json = JSONObject. fromObject (map); this. result = json. toString (); // assign a value to result and pass it to return SUCCESS;} public String getResult () {return result;} public void setResult (String result) {this. result = result ;}}
  
 

Struts. xml configuration:

 
     
      
      
                      
   
    /index.jsp
               
   
    /error.jsp
                                               
                       
  
 

Note: There is another way to configure Action: the action method does not return any value, and the corresponding action configuration does not configure any return type. However, although the action method does not return any value, but you need to write the return value to 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
 
  
Map = new HashMap
  
   
(); Map. put ("word", "word_A"); map. put ("wordcount", 33); JSONObject json = JSONObject. fromObject (map); // converts a map object to a json data pw. write (json. toString (); pw. flush (); pw. close (); return null ;}
  
 

No matter how I try it, this Jquery asynchronous request always returns an error!


You must view the results returned by the page request, open chrome to enter the management interface, and press F12 to debug the interface.

Click the tree node. On the "Network" Tab page, view all requests and corresponding responses. Although the Config. action Request returns a code of 200, it is normal, but the response is abnormal !!!

Then eliminate these exceptions one by one, basically because there are few Jar packages, and the last jar package is as follows:



Success is returned, but the JSON string returned by parsing is always incorrect. My return is as follows: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20150125/20150125094621231.png" alt = "\">


Then we can find the json that can be parsed on the Internet as follows: {"key1 ': 'value1'}, and use the format as follows:

Var obj = eval ("(" + strJSON + ")"); // converted JSON object alert (obj. name); // json name

This method is used for parsing. When I look at the returned json, I find that there is an additional result, so I can parse it in this way:

alert(eval("("+data.result+")").word);

Related Article

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.