Jquery + getJson + struts2, cross-origin access problems

Source: Internet
Author: User
Tags return tag

Problem 1 cross-origin access is unavailable or empty

Problem 2 incorrect cross-origin return tag

I encountered the above problems when using ajax cross-origin access this time. I also found a lot of information on the Internet, but I only mentioned the above or I was not worried about it.

Then, the next day, you can perform the debugging carefully and read the reference manual.

Ajax Solution

 

Expected data type returned by the server. If this parameter is not specified, jQuery will automatically return responseXML or responseText Based on the MIME information of the HTTP package and pass it as a callback function parameter. Available values:

"Xml": returns an XML document, which can be processed by jQuery.

"Html": returns plain text HTML information. The script tag is executed when the dom is inserted.

"Script": returns plain text JavaScript code. Results are not automatically cached. Unless the "cache" parameter is set. ''' Note: ''' during remote requests (not in the same domain), all POST requests will be converted to GET requests. (Because the script tag of DOM will be used for loading)

"Json": Return JSON data.

"Jsonp": JSONP format. When calling a function in the form of JSONP, such as "myurl? Callback =? "Will jQuery be replaced automatically? For the correct function name to execute the callback function.

"Text": returns a plain text string.

Solution code

Var url = 'HTTP: // 192.168.0.25: 8080/peu/webaction. action? Result = ik & format = json & jsoncallback =? ';

$. Ajax ({
Type: "POST ",
Url: url,
Data :"",
DataType: 'jsonp ',
Async: false,
Success: function (msg ){
// Data
// Var data = eval ('+ msg. result + ')');
Var string = msg. result [0];

Alert (string );

},

 

GetJson. getJson is a simplified ajax encapsulation.

Function ajaxList (){

Var url = 'HTTP: // 192.168.0.25: 8080/peu/webaction. action? Result = ik & format = json & jsoncallback =? ';

$. GetJSON (url, function (data ){
// Var obj = eval ('+ data. result + ')');
// Alert (data. result [0]);
Alert (data. result [1]);


});
}

 

 

The front-end is all set up, and even the front-end is inseparable from background support. The previous error is reported.

& Jsoncallback =? This suffix, because it is like this in real requests.


Http: // 192.168.0.25: 8080/peu/webaction. action? Result = ik & format = json & jsoncallback = jsonp136685653745920

Automatically add method name in the browser: jsonp136685653745920

To solve cross-origin return using javascript, return parameters are used, so the return format must be

Jsonp136685653745920 ("+ json_object +"). This is the focus. The returned data format is valid.

 

 

Background

 

 

Public String execute (){
Try
{

String chartData = "bytes, numbers, 83, listen, 13277,14299, 12345,83, listen, 13277,14299, 12345, 12345, listen, 12345, 15763, 12345, 15763, 12000,17733, 9876, 83, 12899,8000 ";
String chartData2 = "9876,83, clerk, 13277,14299, clerk, 9876,83, clerk, 15763,12000, 17733,9876, clerk, 12345,12345, 15763,12000, 17733,9876, 12783,12899, 13888,13277, large, 12345,12345, 15763,12000, 17733,9876, 12783,12899, 13888,13277, large, 26000 ";

List <String> list = new ArrayList <String> ();

List. add (chartData );
List. add (chartData2 );
// Result = JSONUtil. (list );

JSONArray arr = JSONArray. fromObject (list );


String callback = this. getRequest (). getParameter ("jsoncallback ");

// Log.info ("" + callback );

String jsoncallback = callback + "({'result':" + arr + "})";

PrintWriter out = this. getResponse (). getWriter ();

Out. print (jsoncallback );

Out. flush ();
Out. close ();

Log.info ("" + jsoncallback );

}
Catch (Exception e)
{
// TODO: handle exception
}

Return null; // the return value must be null. Otherwise, the format is still incorrect. That is to say, the response is returned through response.
}

 


 

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.