0

我从昨天开始寻找这个,不知道,我无法实施,或者走错了方向。 我目前正在使用本地服务器的 ajax 函数

function tooltipajax(r_ID)
{
    var str  ;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById('span'+ r_ID).innerHTML = xmlhttp.responseText ; 
        }
    }

    xmlhttp.open("GET","accounteditajax.php?key=" +r_ID,true);
    xmlhttp.send();
}

PHP代码:

print("<tr bgcolor=\"#EEEEEF\">");
print("<td class='normal' id=\"serialno\" onMouseOver='tooltipajax(this.id)'>
<a class=\"tooltip\" >Serial Number <span id=\"spanserialno\" 
class=\"custom info\"></span>   </a></td>");
print("<td bgcolor = \"#FFFFFF\" ><b>$serial</b></td>\n");
print("</tr>\n");

如何从另一台服务器获取数据?

xmlhttp.open("GET","accounteditajax.php?key=" +r_ID,true);

我想从

http://iphere/filename.php
4

2 回答 2

0

如果您像这样使用 jQuery,则此方法有效。

function tooltip_ajax(r_ID) {
    $.ajax({
        url: "http://iphere/filename.php?id=" + r_ID,
        context: document.body,
        success: function(data) {
            if(data) {
                $('span' + r_ID).html(data);
            }
        }
    });
}

这是用不同的服务器测试的,它可以工作。

于 2013-05-22T15:37:56.267 回答
0

为了使用 AJAX 从另一个服务器检索数据,您需要使用 JSONP。这是由于 AJAX 请求的跨域限制。为了进一步扩展这一点,如果您想从位于 http://test1.com 的页面向位于 http://test2.com 的页面/脚本发出 AJAX 请求将不被允许。

查看这篇文章了解更多信息:http ://www.jquery4u.com/json/jsonp-examples/

基本上,JSONP 涉及向页面添加一个临时 SCRIPT 标记,该标记可以加载外部内容。此 SCRIPT 标记的 URL 包含数据和回调函数名称。JSONP 然后应该响应数据,包含在对该函数的调用中。当然,一个缺点是目标服务器必须支持 JSONP 请求。

一种替代方法是在本地使用桥接 PHP 脚本,该脚本利用 CURL 发出请求,并通过 AJAX 将信息返回到您的页面。

有关在 PHP 中使用 CURL 的更多信息,请查看这篇文章:http ://codular.com/curl-with-php

于 2013-05-22T15:50:49.867 回答