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.
}