繁体   English   中英

代理连接到 XML 跨服务器..使用 Ajax 在某些浏览器上工作而不是其他浏览器? 包含代码

[英]Proxy to connect to XML cross server..Using Ajax works on some browsers not others? code included

好的,首先让我解释一下我在做什么:

我正在尝试连接到http://www.nfl.com/liveupdate/scorestrip/ss.xml以获取 xml 并允许我直接跨域解析它。 太棒了。。

我正在使用 PHP 通过代理连接到站点,这非常有效

然后回到我的主 HTML 文件中,我正在使用 Ajax 来解析该 XML 文件。 问题是我得到的结果好坏参半。 例如,在我的 macbook pro 上使用所有最新的浏览器(Safari、Firefox、Chrome)这不起作用。 在我的 iPhone 上它可以工作。 并在我的 Mac 桌面上使用所有最新的浏览器。

有谁知道为什么?

另外,我不知道我在用 XML 做什么,这是我第一次尝试学习如何阅读 XML。 所以我可能需要你解释如何更好地解析,因为我现在这样做的方式是来自一位在线用户。

这是有效的 PHP 代理:

<?php

$server_url = "http://www.nfl.com/liveupdate/scorestrip/ss.xml";

$options = array
(
    CURLOPT_HEADER         => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_TIMEOUT        => 60,
    CURLOPT_CONNECTTIMEOUT => 0,
    CURLOPT_HTTPGET        => 1
);

$service = $_GET["service"];

$request_headers = Array();
foreach($_SERVER as $i=>$val) {
        if (strpos($i, 'HTTP_') === 0) {
                $name = str_replace(array('HTTP_', '_'), array('', '-'), $i);
                if ($name != 'HOST')
                {
                    $request_headers[] = "{$name}: {$val}";
                }
        }
}

$options[CURLOPT_HTTPHEADER] = $request_headers;

switch (strtolower($_SERVER["REQUEST_METHOD"]))
{

    case "post":
        $options[CURLOPT_POST] = true;
        $url = "{$server_url}".$service;

        $options[CURLOPT_POSTFIELDS] = file_get_contents("php://input");

        break;
    case "get":

        unset($_GET["service"]);

        $querystring = "";
        $first = true;
        foreach ($_GET as $key => $val)
        {
            if (!$first) $querystring .= "&";
            $querystring .= $key."=".$val;
            $first = false;
        }

        $url = "{$server_url}".$service."?".$querystring;

        break;
    default:
        throw new Exception("Unsupported request method.");
        break;

}

$options[CURLOPT_URL] = $url;

$curl_handle = curl_init();

curl_setopt_array($curl_handle,$options);
$server_output = curl_exec($curl_handle);
curl_close($curl_handle);

$response = explode("\r\n\r\n",$server_output);
$headers = explode("\r\n",$response[0]);

foreach ($headers as $header)
{
    if ( !preg_match(';^transfer-encoding:;ui', Trim($header))  )
    {
        header($header);
    }
}

echo $response[1]; 


?> 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

这是带有 AJAX 的麻烦 HTML 文件:

<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<script>

$.ajax({
    type: "GET",
    url: "http://www.allencoded.com/test3.php",
    dataType: "xml",
    success: function(xml) {
        // Interpret response
        $(xml).find('g').each(function() {

            // Example: Show the XML tag in the console
            console.log(this);

            // Example: Put some output in the DOM
            $("#divOutput").append($(this).attr("hnn"));

        });

        $(xml).find('g').each(function() {



            // Example: Put some output in the DOM
            $("#divOutput").append($(this).attr("vnn"));

        });        

    }
});
</script>

<div id="divOutput"></div>

</body></html>

最后这里是 XML 供参考: http://www.nfl.com/liveupdate/scorestrip/ss.xml

我真的在寻找一种方法来解析它,因为这将是一次很棒的学习体验。 顺便说一句,如果它在我的 macbook 上对 Firefox 有帮助,所有问题都告诉我:括号中的第 12 行缺少)

另外,如果您能以菜鸟可能理解的方式回答我,我将不胜感激,因为我是新手。

谢谢!

Edit: Adding my website links to this code: http://allencoded.com/footballxml.html and http://allencoded.com/test3.php

如果它不是由某些 C&P 问题引起的,则可能是原因:

$(xml).find('g').each(function() {



        // Example: Put some output in the DOM
        $("#divOutput").append($(this).attr("vnn"));

}) //Here is a colon missing!

暂无
暂无

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

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