javascript - same origin policy in external js file - Stack Overflow

if in the websitethere's an external js file added as <script src=".js"><scr

if in the website there's an external js file added as

<script src=".js"></script> 

within the .js js file, there's an ajax call to a script in .js

in such a case will there be the same-origin policy security problem, as it's calling a script in a site from another website?

if in the website http://www.mysite. there's an external js file added as

<script src="http://www.yoursite./new.js"></script> 

within the http://www.yoursite./new.js js file, there's an ajax call to a script in http://www.yoursite./new.js

in such a case will there be the same-origin policy security problem, as it's calling a script in a site from another website?

Share asked Sep 6, 2010 at 10:19 AnishAnish 1,1644 gold badges15 silver badges28 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

There will be a problem. new.js run in the scope of mysite., not yoursite..

EDIT: a more detailed explanation would be: when mysite. is openning a tag, that script runs in the scope of the current page. The source of the script does not matter: it can be inline, local source, or remote source, it is still considered part of mysite.

As you know, scripts in mysite. cannot access anything on yoursite. due to the same origin policy. So you cannot do this.

As an advanced option for cross-origin munication look at jsonp. It will require yoursite. to provide a special handling, but if you have control on both sites then this should not be a problem.

JSONP is precisely what you're looking for: http://en.wikipedia/wiki/JSON

The 5,000m overview is that JSONP uses the same mechanism for requesting external scripts as you're using above. The difference is that your server will recognise this and will package up the JSON response as the argument to a callback method. When your site receives this 'script', it executes it thereby returning the data directly into your callback method.

If you are able to use a framework like jQuery, most of the client side would be transparently handled for you. Check it out here: http://api.jquery./jQuery.getJSON/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744922840a4601242.html

相关推荐

  • javascript - same origin policy in external js file - Stack Overflow

    if in the websitethere's an external js file added as <script src=".js"><scr

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信