Struts2 and Jqurey Ajax in conjunction with cross domain requests

Source: Internet
Author: User


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.

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.