About same origin policy…Can I do this?

若如初见. 提交于 2020-01-23 17:10:24

问题


I have a siteA, and JS in siteB. The siteB JS get the siteB domain to return JSON in AJAX POST request. Then, base on the information in siteB domain, and use the siteB JS inject into site A website via some create document technique in JS. If it is violate the same origin policy, any other suggestions? Thank you.

(I only have the right to control siteB.)


回答1:


People generally think about three origins when dealing with this sort of thing:

  1. The Page (http://example.com/)
  2. The Script (http://example.net/foo.js)
  3. The Data (http://example.net/bar.json)

There is often confusion about which ones are compared when working out if the "same" origin is being used. Only the origin of the page and the data matter. Where the script itself is loaded from is irrelevant.

In this case:

  1. The Page: A
  2. The Script: B (irrelevant)
  3. The Data: B

Since the page and the data come from different origins, you cannot read the data from the page.

Since you want to make a POST request: Proxy the HTTP request through A. (JSON-P, the other classic cross-domain Ajax technique, is strictly limited to GET requests.)

You could also consider using Cross-Origin Resource Sharing which allows you to override the Same Origin Policy, but has limited browser support.




回答2:


you can use a bridge server-side. create a file in php/asp in site A that call (like through curl) your other site B.




回答3:


You can't use javascript from one domain to modify the content of another website on another domain. If you control both sites (and I hope you do), you can use javascript from B to push data to site B's server, and then use inter-server communication to update A's server, and hence update A's content.



来源:https://stackoverflow.com/questions/7163162/about-same-origin-policy-can-i-do-this

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