Ajax
$.ajax ({
URL: "Https://www-xxx.com/xxx/getCounselorDetailByHxCode",
data: {xxx:x},
dataType: ' Jsonp ',
JSONP: "Callback",
type: ' Post ',
error:function (data) {
console.log (data);
},
success: function (data) {
// alert ("suc");
Console.log (data);
},
});
Where datatype specifies that JSONP,JSONP be specified as callback (optional), and then look at the configuration of struts2:
<action name= "Getcounselordetailbyhxcode"
class= "xxx" method= "Getcounselordetailbyhxcode" >
< Result name= "Success" type= "JSON" >
<param name= "Callbackparameter" >callback</param>
< param name= "NoCache" >true</param>
</result>
</action>
Among them, this article
<param name= "Callbackparameter" >callback</param>
Callback is the same as the Jsonp field setting in Ajax
The printed data is the member object that all the structs of the request class instance can get:
Hxcodelist:null
Hxtype:null
Content:null
Counselordetail: "Success"
Counselordetailbyhxcode: "Success"
Spresult:object
Typetypestr:null
Urlstr:null
Userhxcode: "zeKNVeDc208"
Userhxsubcode:null
Userinfoid:null
__PROTO__: Object
JS,
data = Data.spresult;
Con = = ' <br>
To access the results
201611.1 Supplements:
Jsonp a cross-domain can only be a string, so struts should specify a string variable when returning
<result name= "Success" type= "JSON" >
<param name= "root" >jsonpResult</param>
<param Name= "Callbackparameter" >URGOO_CALLBACK</param>
<param name= "NoCache" >true</param>
</result>
Where Jsonpresult is the String type
Public String Actionmethod () throws Exception {
Spresult = Servicemethod ();
Jsonpresult = Jsonutils.convert (spresult);
return SUCCESS;
}
In addition, there is a way to specify a cross-domain:
In the Actionmethod,
HttpServletResponse response = Servletactioncontext.getresponse ();
Response.setheader ("Access-control-allow-origin", "*");
The front-end code at this point doesn't have to change at all.