简体   繁体   English

Javascript API在主站点上运行良好,但在外部服务器上无法运行

[英]Javascript API is working fine on the main site, but fails to work on an external server

This code can be used in conjuction with jQuery to embed a button on a page letting users share a link to my site. 该代码可与jQuery结合使用,以在页面上嵌入一个按钮,让用户共享指向我网站的链接。

<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. javascript文件必须使用xmlHTTPrequest与php文件连接,以检查您是否已登录,然后在我的网站上发布评论。 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 好吧,它在http://mainserver.co.uk的任何页面上都可以正常工作,但是在我的外部服务器上,它只显示控制台错误,例如

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? 好吧,我知道这一定是有可能的,因为Twitter,Google和Facebook都可以做到,那我为什么不能呢?
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 登录并运行此代码后,它在主站点上返回0,但在外部站点上返回1,就好像我仅在主站点上进行测试时才登录

Origin http://externalserver.com is not allowed by Access-Control-Allow-Origin. Access-Control-Allow-Origin不允许使用来源http://externalserver.com

If your JavaScript runs on example.com and the server you are requesting to is in example.net. 如果您的JavaScript在example.com上运行,而您请求的服务器在example.net中。 Make sure it allows the page your js is running by this header 确保它允许此标头运行您的js页面

   <?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标头从Ajax发送到服务器

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

XMLHttpRequest cannot load filename. XMLHttpRequest无法加载文件名。

  • is because you didn't specify full url in you ajax request. 是因为您没有在ajax请求中指定完整的url。 But it works with your main server because you run php in the same domain itself. 但是它可以与您的主服务器一起使用,因为您可以在同一域中运行php。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM