Javascript 小部件在我自己的域上工作正常,但在其他人的域上却不行?
创建了 javascript 小部件。在我自己的域上进行测试并且工作正常。然而,当在第三方网站上发布时,它似乎没有连接到数据库并获取数据。
这是我获取数据的 js 文件部分:
/******* Load HTML *******/
var jsonp_url = "http://www.example.com/widget/data.php";
$.getJSON(jsonp_url, function(data) {
当我在 example.com 上测试时,一切正常。我将 data.php 的权限设置为 777,但它仍然不起作用。请帮忙!
Created javascript widget. Testing on my own domain and its working fine. However when posting on a 3rd party site it looks like it's not connecting to the database and getting the data.
Here is the part of the js file where I get the data:
/******* Load HTML *******/
var jsonp_url = "http://www.example.com/widget/data.php";
$.getJSON(jsonp_url, function(data) {
When I test on example.com everything is fine. I set the permissions on data.php to 777 and it still isn't working. Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能跨不同域进行 ajax 调用:
假设您的域名是“example.com”,第三方网站的域名是“thirdparty.com”。您在thirdparty.com 上安装该小部件。 Thirdparty.com 上的小部件代码将尝试向“example.com”发出 ajax 请求。这是浏览器禁止的。
您始终可以用直接的 < 来替换 ajax 调用。脚本>标签。这没有任何限制。
希望有帮助
you cannot make an ajax call across different domains:
let's say your domain is 'example.com', and the third party site has 'thirdparty.com'. you install the widget on thirdparty.com. The widget code on thirdparty.com will try to make an ajax request to 'example.com'. Which is forbidden by the browser.
You can always replace the ajax call with a straight < script > tag. This doesn't have any restriction.
Hope it helps
您有一个名为
jsonp_url
的变量,但您使用的 URL 不包含字符串callback=?
,其中 文档说触发 jsonp 模式。您需要将其包含在 URL 中,并确保您的服务器端脚本输出 JSONP(使用
$_GET['callback']
(经过适当的清理)来确定将 JSON 包装在其中的函数名称)。You have a variable called
jsonp_url
, but the URL you use doesn't include the stringcallback=?
which the documentation says triggers jsonp mode.You need to include that in the URL and make sure that your server side script is outputting JSONP (using
$_GET['callback']
(with suitable sanitisation) to determine the function name you wrap the JSON in).他们需要启用跨源资源共享。 http://enable-cors.org/
they need to enable cross orgin resource sharing. http://enable-cors.org/