分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

web跨域问题

发布时间:2023-09-06 02:12责任编辑:胡小海关键词:跨域

  Recently, I joined on a program about the chrome extension and met with some problem that is about web cross doamin. so I collect some information about this.

  First, We must know what is cross domain. Popularly, cross domain is that a domain sends a request to another domain. There are some performances.

    1: same domain, but not port. Like this, www.domain.com:80,  www.domain.com:8080.

    2: same domain, but not scheme. Like this, http://www.domain.com, https://www.domain.com.

    3: the domain with corresponding ip. Like this, http://www.domain.com, http://128.112.1.21/.

    4: different domain.

    5: subdomains are different.

  As mentioned above, there are all cross domain,  however, why has cross domain? please following up with me.

  Now we must mention a word, SOP(same origin policy), this is about web browser safety, if not sop,  we maybe afford some hack behavoirs, like Xss, CSFR...

  So, there are some SOP limits to web browser:

    1: can‘t send ajax

    2: can‘t read cookie, indexDB or something

    3: dom and js object can‘t be got.

  But we can move around by some solutions.

    1:jsonp. we cant create a tag script and request to domain.

    

1 var scriptTag = document.createElement("script");2 scriptTag.src = "http://www.domain.com?user=admin&callback=callBackFunction";3 scriptTag.type = "text/javascript";4 document.head.appendChiild(scriptTag);5 6 7 function callBackFunction(data){8 ???console.log(data); ??????????9 }

    also, you can use Jquery. but the server must return callback function .

    2:  postMessage. 

      targetWindow.postMessage(message, origin). you can use parent window to open another window and post message to it, but the child window must use window.addEventListener to llisten the message. however, sometimes, the child window maybe listen a lot of message, you shouldn‘t use the origin with *,  and attention, the child window must be fully loaded ,the parent window can send message to it ,otherwise the child window can‘t get message.

    

1 // parentWinow2 childWindow.postMessage(data, "domain");3 4 //child window5 window.onload = function(){6 ????window.addEventListener("mesage", function(data){7 ????????console.log(data);8 ????}) ?9 }

    3: cors. maybe this method is common .  you need to set ajax request with withCredentials and set property(Access-Control-Allow-Credentials) is true, 

      and property(Access-Control-Allow-Origin) to yuor domain.

     

Also, there are some resolutions to solve with the problem. And for chrome extension, don‘t add request domain to permissions(manifest.json), because of the security for customer.

  

  

web跨域问题

原文地址:https://www.cnblogs.com/TigerandRose/p/9569838.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved