Javascript API第正在主站点上的罚款,但没有一个外部服务器上运行 [英] Javascript API is working fine on the main site, but fails to work on an external server

查看:103
本文介绍了Javascript API第正在主站点上的罚款,但没有一个外部服务器上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code可以使用jQuery一起选择可以用来嵌入让用户共享一个链接到我的网站页面上的一个按钮。

 < D​​IV CLASS =按钮数据文本=按钮文本数据URL =htt​​p://externalserver.com>
< / DIV>
&所述; SCRIPT SRC =htt​​p://mainserver.co.uk/api.js>&下; /脚本>

JavaScript文件必须使用xmlHTT prequest与PHP文件连接到检查您是否已登录与否,并然后张贴在我的网站的评论。这是非常简单的,因为所有你需要做的就是点击创建按钮。还是不行。结果
那么,它工作正常的任何网页上 http://mainserver.co.uk ,但我的外部服务器上,它只是显示控制台的错误,比如

  XMLHtt prequest无法加载文件名。原产地http://externalserver.com没有被访问控制允许来源允许的。

嗯,我知道这一定是可能的,因为Twitter的,谷歌和Facebook都可以做,所以我为什么不能?结果
修改的结果
好,谢谢。但是这仍是一个问题。结果
用户可以在它告诉他们登录在网页中mainsite会话。结果
使用这种code,我可以测试。

 < PHP
在session_start();
标题(访问控制允许来源:*);
如果(使用isset($ _ SESSION [用户]))
{
    回声0;
}
其他
{
    回声1;
}
?>

当我登录并运行该code,它在外部网站主网站上,则返回0,但1,好像我只是记录在主站点上测试时


解决方案

  

http://externalserver.com 不被容许
  访问控制允许来源。



  • 的跨域AJAX的问题(阅读更多:同源策略,<一个HREF =htt​​p://en.wikipedia.org/wiki/XMLHtt$p$pquest#Cross-domain_requests相对=nofollow>跨域请求的)

如果您的JavaScript运行在example.com和你请求到服务器是example.net。确保它可以让你的JS运行的页面这个标题

 &LT; PHP的头?('访问控制允许来源:http://example.com/ajax.html'); ?&GT;

 &LT; PHP的标题(访问控制允许来源:*')?; ?&GT;

(通配符会允许任何域将请求发送到您的主机。我建议与你上运行脚本的特定域替换星号)

此外,你需要从阿贾克斯送Origin标头的服务器

 产地:http://example.com/ajax.html


  

XMLHtt prequest无法加载文件名。



  • 是因为你没有在你的Ajax请求指定完整的URL。但它与你的主服务器,因为你运行在同一个域本身的PHP工作。

This code can be used in conjuction with jQuery to embed a button on a page letting users share a link to my site.

<div class="button" data-text="Button text" data-url="http://externalserver.com">
</div>
<script src="http://mainserver.co.uk/api.js"></script>

The javascript file has to use xmlHTTPrequest to connect with php files to check whether or not you are logged in or not, and to then post the comment on my site. It is very simple, as all you need to do is click the button it creates. Or not.
Well, it works fine on any page on http://mainserver.co.uk, but on my external server, it just shows console errors such as

XMLHttpRequest cannot load filename. Origin http://externalserver.com is not allowed by Access-Control-Allow-Origin. 

Well I know this must be possible because twitter, google and facebook can all do it, so why can't I?
EDIT
Ok, thanks. but this is still a problem.
Users have sessions on the mainsite which tells the webpage that they are logged in.
Using this code, I can test that.

<?php
session_start();
header("Access-Control-Allow-Origin: *");
if (isset($_SESSION["user"]))
{
    echo "0";
}
else
{
    echo "1";
}
?>

When I am logged in and run this code, it Returns 0 on the main site, but 1 on the external site, as if I am only logged in when testing on the main site

解决方案

Origin http://externalserver.com is not allowed by Access-Control-Allow-Origin.

If your JavaScript runs on example.com and the server you are requesting to is in example.net. Make sure it allows the page your js is running by this header

   <?php header('Access-Control-Allow-Origin: http://example.com/ajax.html'); ?>

or

   <?php header('Access-Control-Allow-Origin: *'); ?>

(the wildcard is going to allow any domain to send requests to your host. I recommend replacing the asterisk with a specific domain that you will be running scripts on)

Also you need to send Origin header from ajax to the server

Origin: http://example.com/ajax.html

XMLHttpRequest cannot load filename.

  • is because you didn't specify full url in you ajax request. But it works with your main server because you run php in the same domain itself.

这篇关于Javascript API第正在主站点上的罚款,但没有一个外部服务器上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆