There are two cases of cross-domain JavaScript: 1. Between subdomains based on the same parent domain, such as: a.c.com and b.c.com 2. Between subdomains based on different parent domains, such as: www.a.com and www.b.com 3. The port is different, such as: www.a.com:8080 and www.a.com:8088 4. The protocol is different, such as: http://www.a. com and https://www.a.com For situations 3 and 4, it needs to be solved through a background proxy. The specific method is as follows: a. Create a proxy program under the initiator's domain b. The initiator's js calls the proxy program under this domain c. The proxy sends the request to the receiver and obtains the corresponding data d. The proxy returns the obtained data to the initiator's js The initiator page code As follows:
using System.Data; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Linq; using System.IO; using System.Net; using System.Text; namespace WebApplication1 { /// /// Summary description for $codebehindclassname$ // / [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Proxy : IHttpHandler { const int BUFFER_SIZE = 8 * 1024; public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string src = context.Request ["src"]; WebRequest wr = WebRequest.Create(src); WebResponse wres = wr.GetResponse(); Encoding resEncoding = System.Text.Encoding.GetEncoding("gb2312"); StreamReader sr = new StreamReader(wres.GetResponseStream(), resEncoding); string html = sr.ReadToEnd(); sr.Close(); wres.Close(); context.Response.Write("
"); context.Response.Write(html); } public bool IsReusable { get { return false; } } } }
In addition to using background proxy, there are 7 ways to solve situations 1 and 2: 1. document.domain iframe (only case 1 can be solved): a. On the initiator page and Set document.domain on the receiver page and set the value to the main domain name of the parent domain (window.location.hostname) b. Create a hidden iframe on the initiator page. The source of the iframe is the receiver page c. Depending on the browser, obtain the content of the recipient page through iframe.contentDocument || iframe.contentWindow.document d. Interact with the recipient through the obtained content of the recipient page This One drawback of this method is that when one domain is attacked, security holes will appear in another domain. The initiator page code is as follows:
7. window.navigator (applicable to IE6 and 7, seems to be still available and has not been patched yet) a. The initiator page creates a hidden iframe, and the source points to the recipient page b. The initiator page uses window.navigator.a = function(params){...}; window.navigator.b = function(params){...}; to define the method called by the receiver c. The receiver page calls the method defined by the initiator through window.navigator.a(params); window.navigator.b(params); d. The receiver page uses window.navigator.c = function( params){...}; window.navigator.d = function(params){...}; to define the method called by the initiator e. The initiator page passes window.navigator.c(params); window.navigator.d(params); to call the method defined by the receiver The initiator page code is as follows:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...
There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.
JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.
In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...
JavaScript can be run in PowerPoint, and can be implemented by calling external JavaScript files or embedding HTML files through VBA. 1. To use VBA to call JavaScript files, you need to enable macros and have VBA programming knowledge. 2. Embed HTML files containing JavaScript, which are simple and easy to use but are subject to security restrictions. Advantages include extended functions and flexibility, while disadvantages involve security, compatibility and complexity. In practice, attention should be paid to security, compatibility, performance and user experience.
Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.