Javascript API 在主站点上运行良好,但无法在外部服务器上运行 [英] Javascript API is working fine on the main site, but fails to work on an external server

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

问题描述

此代码可与 jQuery 结合使用,在页面上嵌入一个按钮,让用户共享指向我网站的链接.

<script src="http://mainserver.co.uk/api.js"></script>

javascript 文件必须使用 xmlHTTPrequest 与 php 文件连接以检查您是否登录,然后在我的网站上发表评论.这非常简单,因为您只需单击它创建的按钮即可.与否.
嗯,它在 http://mainserver.co.uk 上的任何页面上都可以正常工作,但在我的外部服务器上,它只显示控制台错误,例如

XMLHttpRequest 无法加载文件名.Access-Control-Allow-Origin 不允许 Origin http://externalserver.com.

好吧,我知道这一定是可能的,因为 twitter、google 和 facebook 都可以做到,那我为什么不能呢?
编辑
好,谢谢.但这仍然是一个问题.
用户在主站点上有会话,告诉网页他们已登录.
使用此代码,我可以对其进行测试.

当我登录并运行这段代码时,它在主站点上返回 0,但在外部站点上返回 1,好像我只是在主站点上测试时才登录

解决方案

Origin http://externalserver.com 不允许访问控制允许来源.

  • 这是一个跨域 ajax 问题(阅读更多:同源策略跨域请求)

如果您的 JavaScript 在 example.com 上运行,并且您请求的服务器在 example.net 中.确保它允许您的 js 通过此标头运行的页面

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

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

(通配符将允许任何域向您的主机发送请求.我建议将星号替换为您将在其上运行脚本的特定域)

还需要将 Origin 标头从 ajax 发送到服务器

来源:http://example.com/ajax.html

<块引用>

XMLHttpRequest 无法加载文件名.

  • 是因为您没有在 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天全站免登陆