Document directory
[ASP. NET] the ASP. NET Ajax webpage is embedded in IFRAME across domain
Summary
ASP. net Ajax is an extraordinary/partially refreshed Ajax framework developed by microservices in the last three years. net Framework Version 2.0 applies to extensions, while.. NET Framework 3.5 is imported into the framework, which applies to ASP. NET programming framework based on development is a great boon. After reading ASP. net Ajax Showcase (http://www.asp.net/ajax/showcase/) really don't want to use.
This framework is very simple and straightforward to use, so it won't take long before it was launched because we don't have to compile the Ajax function by ourselves, it seems that this framework is not easy to understand. However, this framework is even better and has limits. The following lists the missing points of several frameworks:
1. Use a single Ajax function to download a script resource of 80-kb.
2. when the updatepanel refresh parameter is set to full page PostBack, that is, when a part of the refresh occurs, the code of the entire backend will be re-routed to the last refresh line, the difference between PostBack and general is only in that when the response is sent to the client, the response won't be completely refreshed, in fact, this is a major problem for the efficiency of calling a single function (through pagemethods ).
3. When a website is embedded in an IFRAME, a rejected access error may occur.
. Both points will be mentioned in this article, but the focus of this document is on how to solve 3 ..
Problem background
In the near future, product A has been integrated into the two products, and UI integration is implemented to embed product B's internal content through IFRAME, the embedded faces use ASP. net Ajax function. The architecture of the website is roughly as follows:
Mainpage. aspx is the external webpage of product A, ajaxpage. aspx is the network of product B. It is a simple system integration method. It is reasonable to say that there will be no problems, however, in actual operation, this often occurs.
Case Description
The following are some examples of the problems with this design architecture. We first set up a website named ajaxpage on website. aspx, and the following controls are placed in the webpage: scriptmanager, updatepanel, label, button, dropdownlist, and checkbox:
Let's take a look at it first. It's very good if everything works!
Then we create a website called mainpage on website B. aspx, and add an IFRAME to the webpage to specify src to the ajaxpage of website. aspx, as follows:
At this time, I opened mainpage. aspx, everything seems to be normal, very good, and I cannot help but feel happy. This cross-system integration is easy to complete. I am planning to say that the integrated functions are good when I am about to make a gap, it's okay if you don't get angry. When I click the button on the selected page, the following response message is displayed:
According to our experiences, we found that no matter which component on the selected page will jump out of this kind of warning message, so we can't help but think that it must be ajaxpage. when aspx crashes, go back to restore your ajaxpage. aspx, I found that there is no problem at all. Based on the spirit of knowledge, we will return to mainpage. aspx case occurs. When it jumps out again, we don't want to give it a bit of "yes 』.
It's not good. I clicked on a real speaker. We can see the following picture, which makes people feel strange. Generally, when we develop a script program, shouldn't it be in the script? What is this scriptresource. axd? The point is that we still cannot find it. How can this problem be solved? In the next chapter, we will introduce these strange new things.
ASP. NET Ajax ResourceScriptresource. axd
Scriptresource refers to the resource definition that includes scripts, while ASP. the design mode of net Ajax is to embed the JS libraries provided by all frameworks into scriptresource in the form of content. in the axd case, we can see the content of the website as follows after we save another website:
There are two scriptresources and one webresource. The purpose of webresource is not described in this file. If you are interested, you can take the test (Summary 1), Asp. the JS cases used by net Ajax are completely invisible here, because these cases are all embedded in scriptresource. Before resolving the JS scripts in scriptresource, let's take a look at why we need to embed the JS plugin using scriptresource:
1. Automatically gzip/compressing your scripts over HTTP for delivery.
2. dynamically resolving release/debug scripts based on build parameters. This is useful, if you keep two types of the same script: one for debug, and one packed for release.
The first reason is that scriptresource can be used to initiate a trial, reducing the number of website connections; the second reason is that JavaScript code can be divided into debug and non-DEBUG Versions. It determines whether the script embedded in scriptresource is a debug version based on the number of tests, so what is this score? Let's look at it next time.
(Valve 1: http://bchavez.bitarmory.com/archive/2008/07/28/understanding-scriptresource-and-webresource-in-asp.net.aspx)
ASP. NET Ajax JS Environment
First, we will go to ASP. NET Ajax's security path. The JS location is as follows:
We can see that there are a total of six JS engines, each of which has a debug version and a non-debug version, the differences between the two versions can be seen from the case size, microsoftajax. debug. JS and microsoftajax. the JS case size is about KB. Let's look at the content of the two cases:
First, microsoftajax. Debug. JS:
Microsoftajax. JS:
The content of these two cases is exactly the same. The difference is that only the debug version of the program can be formatted, rather than the debug version, the reason is that we only care about the JS typographical content in debug mode. For convenience, the official release version does not require typographical layout, as long as the program is not ready for operation. However, there is no typographical structure in a single statement, and the size of the two statement cases is about KB. Based on this, we must select the release version for the official release system, how to choose? Set debug in Web. config to false.
Solution
ASP. net Ajax resource content, we still want to return to our original question: When ASP. net Ajax is embedded in IFRAME in the form of a cross-domain. What should we do when the following death occurs?
Solution description
When zookeeper is in microsoftajax. debug. JS, but all JS scripts are wrapped in scriptresource, and these JS scripts are automatically added to scriptresource by framework. in axd, how do we modify this question?
Fortunately, the micro-engine project team helped us to leave some paths behind and get wrapped in scriptresource. JS scripts in axd can be modified through a program. We only need to correct the JS issue and re-specify it to scriptresource. axd is enough.
Root Cause
The content of this line is as follows:
There is a problem with this method. The main problem is that when a top object is obtained across the network, the top object will be destroyed due to insufficient permission. We can imagine that, if we have a website that embeds Yahoo's website, then our website can operate Yahoo's objects intentionally, this is so terrible that it is not allowed to perform operations similar to cross-domain access to a website object.
Modification Method
<Step1>
We are at microsoftajax. in JS (the reason for not selecting the debug version is that this case is too small), the case sys. browser. internetexplorer: With case sys. browser. safari: The program between them is compiled into the following content:
| |
| |
01 SYS. UI. domelement. getlocation = function (element ){
02 if (element. Self | element. nodetype = 9) return New SYS. UI. Point (0, 0 );
03 var clientrect = element. getboundingclientrect ();
04 {
Document. getelementbyid ('codefunction6608 _ shrink_img '). style. display = 'inline'; document. getelementbyid ('codefunction6608 _ expand_img '). style. display = 'none'; document. getelementbyid ('codefunction6608 _ expand_text '). style. display = 'none'; document. getelementbyid ('codefunction6608 _ shrink_text '). style. display = 'inline'
} "Src =" http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedBlockStart.gif "alt =" "align =" TOP "> {
Document. getelementbyid ('codefunction6608 _ shrink_img '). style. display = 'none'; document. getelementbyid ('codefunction6608 _ expand_img '). style. display = 'inline'; document. getelementbyid ('codefunction6608 _ expand_text '). style. display = 'inline'; document. getelementbyid ('codefunction6608 _ shrink_text '). style. display = 'none ';
} "Src =" http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ContractedBlock.gif "alt =" "align =" TOP "> If (! Clientrect )...{
05 return New SYS. UI. Point (0, 0 );
06}
07 var ownerdocument = element.document.doc umentelement;
08 var offsetx = clientrect. Left-2 + ownerdocument. scrollleft,
09 offsety = clientrect. Top-2 + ownerdocument. scrolltop;
10
11 {
Document. getelementbyid ('codefunction4487 _ shrink_img '). style. display = 'inline'; document. getelementbyid ('codefunction4487 _ expand_img '). style. display = 'none'; document. getelementbyid ('codefunction4487 _ expand_text '). style. display = 'none'; document. getelementbyid ('codefunction4487 _ shrink_text '). style. display = 'inline'
} "Src =" http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedSubBlockStart.gif "alt =" "align =" TOP "> {
Document. getelementbyid ('codefunction4487 _ shrink_img '). style. display = 'none'; document. getelementbyid ('codefunction4487 _ expand_img '). style. display = 'inline'; document. getelementbyid ('codefunction4487 _ expand_text '). style. display = 'inline'; document. getelementbyid ('codefunction4487 _ shrink_text '). style. display = 'none ';
} "Src =" http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ContractedSubBlock.gif "alt =" "align =" TOP "> try ...{
12 var F = element. ownerdocument. parentwindow. frameelement | NULL;
13 {
Document. getelementbyid ('codefunction5517 _ shrink_img '). style. display = 'inline'; document. getelementbyid ('codefunction5517 _ expand_img '). style. display = 'none'; document. getelementbyid ('codefunction5517 _ expand_text '). style. display = 'none'; document. getelementbyid ('codefunction5517 _ shrink_text '). style. display = 'inline'
} "Src =" http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedSubBlockStart.gif "alt =" "align =" TOP "> {
Document. getelementbyid ('codefunction5517 _ shrink_img '). style. display = 'none'; document. getelementbyid ('codefunction5517 _ expand_img '). style. display = 'inline'; document. getelementbyid ('codefunction5517 _ expand_text '). style. display = 'inline'; document. getelementbyid ('codefunction5517 _ shrink_text '). style. display = 'none ';
} "Src =" http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ContractedSubBlock.gif "alt =" "align =" TOP "> If (f )...{
14 var offset = 2-(F. frameborder | 1) * 2;
15 offsetx + = offset;
16 offsety + = offset;
17}
18}
19 {
Document. getelementbyid ('codefunction5699 _ shrink_img '). style. display = 'inline'; document. getelementbyid ('codefunction5699 _ expand_img '). style. display = 'none'; document. getelementbyid ('codefunction5699 _ expand_text '). style. display = 'none'; document. getelementbyid ('codefunction5699 _ shrink_text '). style. display = 'inline'
} "Src =" http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ExpandedSubBlockStart.gif "alt =" "align =" TOP "> {
Document. getelementbyid ('codefunction5699 _ shrink_img '). style. display = 'none'; document. getelementbyid ('codefunction5699 _ expand_img '). style. display = 'inline'; document. getelementbyid ('codefunction5699 _ expand_text '). style. display = 'inline'; document. getelementbyid ('codefunction5699 _ shrink_text '). style. display = 'none ';
} "Src =" http://www.dotblogs.com.tw/Providers/BlogEntryEditor/FCKeditor/editor/dialog/InsertCode/codeimages/ContractedSubBlock.gif "alt =" "align =" TOP "> catch (Ex )...{
20}
21 return New SYS. UI. Point (offsetx, offsety );
22}
23 break; |
|
<Step2>
Upload microsoftajax. js to a project on the platform, such as my example. I put it under ajax_js under the platform,
<Step3>
Modify ajaxpage. aspx, find the scriptmanager on the website, and add the following content:
|
<Asp: scriptmanager id = "scriptmanager1" runat = "server"> 1 <scripts>
2 <asp: scriptreference
3 name = "microsoftajax. js" scriptmode = "Auto"
4 Path = "~ /Ajax_js/microsoftajax. js "/>
5 </scripts>
</ASP: scriptmanager> |
|
When we add the above content, the JS scripts embedded in scriptresource will be changed to the scripts we specified, let's take a look at your website. It's amazing that you haven't been connected to it.
However, because of this problem, we must consolidate all ASP. net Ajax is too labor intensive, so we think of another solution, in our program's superclass oninit, enter the following program example, and the background will automatically help us to add this scriptreference:
| |
| |
1 scriptreference tref = new scriptreference ("~ /Ajax_js/microsoftajax. js ");
2 This. scriptmanager1.scripts. Add (tref ); |
With a set of functions, it is true that all programs have no problems.
Postscript
This issue is a known issue in the developer's blog, but in. net Framework 2.0 Ajax extensions has not been resolved,. net Framework 3.5 has been excluded. If the scheme permits, you can directly upgrade to version 3.5 for exclusion.
Posted on: http://www.dotblogs.com.tw/jimmyyu/archive/2009/04/21/8116.aspx
Reference: http://blog.darkthread.net/blogs/darkthreadtw/archive/2007/04/13/kb-cross-domain-access-denied-issue-of-asp-net-ajax-page.aspx
Thank you for sharing your experience!