Why is the 'src' attribute allowed to link to scripts from external domains, and XmlHtppRequests not?

微笑、不失礼 提交于 2019-12-12 15:12:30

问题


I have read several answers on StackOverflow regarding same-origin policy, but I don't seem to graps the essential part.

In all tags that use the src attribute, like <script> and <img>, you are allowed to use external resources (from another domain).

Why is this allowed, but with a XMLHttpRequest (e.g. AJAX calls) it is not. I do not seem to graps why the latter is more dangerous.

I mean, you could also have malicious code in an external source like: <script src="http://example.com/malicious_script.js"></script>


回答1:


The same-origin policy aims to protect the remote server's data from an unknown client, not to protect the client from malicious code from the server. <script> tags do not allow the client to make requests other than GETs or to obtain data that is not explicitly exposed by the server in a valid JavaScript file.



来源:https://stackoverflow.com/questions/27519546/why-is-the-src-attribute-allowed-to-link-to-scripts-from-external-domains-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!