HttpClient jQueryajax cross-origin access

Source: Internet
Author: User
First, let's look at the background of the problem: website A uses iframe to reference A static html of website B. Part of html of website B needs to be obtained through website C. Here I need to implement Part B to C. Technical Architecture used: struts2, spring, ibatis, and jquery. Try to introduce js remotely. For example, http ://


First, let's look at the background of the problem:


Website A uses the iframe method to reference A static html of website B. Part of the html content of website B needs to be obtained through website C. Here I need to implement Part B to C.

Technical Architecture used: struts2, spring, ibatis, and jquery.

Try to introduce js remotely. For example, http://www.c.com/.mongomongo.js.execute the ajaxrequest in C.

$. Ajax ({
Url: '/showAjax. action ',
Data: 'lotteryid = '+ lotteryId,
Type: 'post ',
DataType: 'json ',
Success: function (json ){
...,
// Alert (json );
Return *;
}
});
Later I found myself making a mistake. In jquery ajax success, return cannot be executed.

The following is a summary of your implementation (web Proxy ):

1. web-side proxy, that is, the cross-origin access requests generated by the user to access website B are submitted to the specified page of website B, which replaces the user page for interaction, return the appropriate results.

2. Cross-origin security restrictions refer to cross-origin security restrictions on the browser side. The server side does not have cross-origin security restrictions. Therefore, the local server side uses the httpclient-like method to complete cross-origin access,

Then, use AJAX on the browser side to obtain the url corresponding to the "cross-origin access" on the local server side. This is also possible for indirect cross-origin access. However, it is clear that the development volume is large, but the limit is also minimal.

Paste the following code:

1. B website front-end:

$. Ajax ({
Url: '/showCpAjax. action ',
Data: 'lotteryid = '+ lotteryId,
Type: 'post ',
DataType: 'json ',
Success: function (json ){
Var periodStr = json. period;
Var timeStr = json. endTime;
// Ignore some code here
}
});
2. ajax submits the request to the background of website B:
Public String showCpIndex (){
String url = "http://www.c.com/?##.do"; // request the csite
HttpClient client = new HttpClient ();
GetMethod get = new GetMethod (url );
Try {
Int statusCode = client.exe cuteMethod (get );
If (statusCode = HttpStatus. SC _ OK ){
String response = get. getResponseBodyAsString ();
ServletActionContext. getResponse (). setCharacterEncoding ("UTF-8 ");
// Respose is the data returned by the request to the csite.
} Else {
//***
}
} Catch (Exception e ){
E. printStackTrace ();
}
Return SUCCESS;
}
3. The csite is just a common struts2action method, and the returned view is a common JSP. All content in jsp is returned to B as a reponse.
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.