Jquery cross-origin calls to struts2 Action to return json data prone to Problems

Source: Internet
Author: User
A recent project used JQuery to call the json data returned by Struts2 Action. Because cross-origin operations are involved, the getJson method that is compatible with cross-origin operations has been selected, however, in actual application, it is found that jquery, which is also purchased by the front end, can call the background Action class successfully... syntaxHighlighter. a

A recent project used JQuery to call the json data returned by Struts2 Action. Because cross-origin operations are involved, the getJson method that is compatible with cross-origin operations has been selected, however, in actual applications, it is found that jquery, which is also purchased by the front end, can call the background Action class successfully, but it is always unable to call the callback function!


After constant search, the cause is finally determined. To successfully call the callback function in a cross-origin call, the server must explicitly know the callback function address. Therefore, there are two necessary elements:

1. callback = ?, This name can be set by yourself. It is not necessarily called, but it must be

2. The struts2 configuration file must contain Jsoncallback

The name of the callback parameter is the same as that passed by the front-end.

The following shows the code snippet I used:

Front-end page: www.2cto.com

$. GetJSON ('HTTP: // xxxxxxx.xhtml? '+ $ (' # RegForm '). serialize () + "& jsoncallback =? ", Function (data ){
Alert (data );
});


Back-end struts-json.xml calibration return value part, note to be included in the result segment

Jsoncallback

Author: mcbeath

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.