简体   繁体   English

如何让 ajax 像 Google Map API 一样工作?

[英]How do I get ajax to work like the Google Map API?

I'm trying to get access to a Javascript API, I created, on other sites.我正在尝试访问我在其他网站上创建的 Javascript API。 The javascript is at https://ksc105.kscserver.com/query.js and it pulls ajax calls to https://ksc105.kscserver.com/suggestions.php (?action=getall). The javascript is at https://ksc105.kscserver.com/query.js and it pulls ajax calls to https://ksc105.kscserver.com/suggestions.php (?action=getall). Of course using this on https://ksc105.kscserver.com/index.php works.当然,在https://ksc105.kscserver.com/index.php上使用它。

However I'm trying to use import that javascript into another domain site.但是我正在尝试将 javascript 导入另一个域站点。 I know cross-domain ajax calls do not work, but I supposed that if the ajax call is made from a javascript on that site that it was going to work.我知道跨域 ajax 调用不起作用,但我认为如果 ajax 调用是从该站点上的 javascript 发出的,它将起作用。 I supposed this based on Google's Map API.我认为这是基于谷歌的 Map API。 I'm pretty sure it uses ajax.我很确定它使用 ajax。

How do I get ajax to work like the Google Map API?如何让 ajax 像 Google Map API 一样工作? Where any website can add my script and use its functions?哪里有网站可以添加我的脚本并使用它的功能?

In firebug, I get the request to fire but I just get an empty return but it should not be returning empty.在萤火虫中,我收到了触发请求,但我得到了一个空的回报,但它不应该是空的。 In IE9, I get the error "SCRIPT5: Access is denied."在 IE9 中,我收到错误“SCRIPT5:访问被拒绝”。 / "query.js, line 62 character 5". /“query.js,第 62 行字符 5”。

If you go to https://ksc105.kscerver.com/index.php and type 3 or more characters in the box you should get "suggestions" much like Google Search.如果您从 go 到https://ksc105.kscerver.com/index.php并在框中键入 3 个或更多字符(如 Google 搜索),您应该会得到“uggs 3 或更多字符”。 I need the same thing to work on any other website without a server proxy.我需要同样的东西才能在没有服务器代理的任何其他网站上工作。 You can use "Test" as it pulls a bunch of test data.您可以使用“测试”,因为它会提取一堆测试数据。

Try using AJAX callbacks.尝试使用 AJAX 回调。 jQuery does this well but as a raw example, if you load some JSON with a callback function (From a <script> tag) it will run the function. jQuery does this well but as a raw example, if you load some JSON with a callback function (From a <script> tag) it will run the function. This is how you can use the Twitter API across sites.这就是您可以跨站点使用 Twitter API 的方式。 IE if you call http://api.twitter.com/1/statuses/user_timeline.json?screen_name=twitter&count=20&callback=handletwitter and create a handletwitter function: IE if you call http://api.twitter.com/1/statuses/user_timeline.json?screen_name=twitter&count=20&callback=handletwitter and create a handletwitter function:

<script type="text/javascript">
function handletwitter(data){
    console.log(data);
}
</script>
<script type="text/javascript" src="http://api.twitter.com/1/statuses/user_timeline.json?screen_name=twitter&count=20&callback=handletwitter"></script>

This is also known as JSONP这也称为 JSONP

There's a smart solution to do so.有一个聪明的解决方案可以做到这一点。 You can have an iFrame of width and height 0, so it won't be visible.你可以有一个宽度和高度为 0 的 iFrame,所以它是不可见的。 From within it, you can load data on main page using 'parent' property.在其中,您可以使用“父”属性在主页上加载数据。 Since you are allowed to load anything in an iFrame, this should be a good solution for you.由于您可以在 iFrame 中加载任何内容,因此这对您来说应该是一个很好的解决方案。 Consider the following example (will also work on different domain).考虑以下示例(也适用于不同的域)。

<html>
    <head>
    <script type="text/javascript">
        function loadData(data)
        {
            var a = document.getElementById("H");
            a.innerHTML = data;
        }
    </script>   
    </head>
    <body>
    <div id="H">hello</div>
    <iframe src="sample.html" width="0px" height="0px">
    </iframe>
    </body>
</html>

Sample.html样品.html

    <html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<script>
    <!--
    parent.loadData("I am inside iFrame");
    -->
</script>
</body>
</html>

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

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