douzhixun8393 2014-12-11 12:32
浏览 455
已采纳

http://www.website.com和http://website.com之间有什么区别?

consider i have created a website and have a web directory like this...

1.index.php

2.ajax.php

In index.php coding.. i use

<html>
....
<base href="http://website.com">
....
....
<script> <!-- using jquery -->
.... 
$.ajax({
        url:'ajax.php',
        type:'POST',
        data:{ 'variable': value  },
        success: function(res){
        ....
        ....    
        }
    });  
....
</script>

when i run above code it works fine... but when i add "www" in base element href attribute, like this...

<base href="http://www.website.com">

its not working why?, it show error like this in javascript..

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://example.com/ajax.php. This can be fixed by moving the resource to the same domain or enabling CORS.

now i have question?

  1. what is the real problem?

  2. what should i do to make both base element code should run?

  3. what is CORS enabling?

  4. if i enable CORS, anyone from other website can able to access my site?

I would appreciate if some one could assist me, thanks in advance...

  • 写回答

2条回答 默认 最新

  • douduan5753 2014-12-11 12:37
    关注

    what is the difference between http://www.website.com and http://website.com?

    They have different hostnames and are (technically) different websites. They could host different content, but probably don't.

    what is the real problem?

    Different hostnames are different origins. Browsers do not let JavaScript running in a page on one origin read content from another origin unless permission is given with CORS.

    what should i do to make both base element code should run?

    Pick one of the two hostnames to be canonical. Configure the server so the non-canonical one issues a 301 HTTP redirect to the canonical one.

    While you are at it, stop using <base>. It is far more pain than it is worth, and almost everything good about it can be achieved by using URLs that are relative to the server root (i.e. URLs that begin with a / character).

    what is CORS enabling?

    Configuring the server to send HTTP headers that give the browser permission to share your site's data with other sites.

    See MDN for more.

    if i enable CORS, anyone from other website can able to access my site?

    You can specify global access or limit it to specific origins.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题