代理连接到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行

如果您能以一个新手可以理解的术语来回答我,我将非常感激,因为我是一个新手。

谢谢!

编辑:将我的网站链接添加到此代码:http://allencoded.com/footballxml.html和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!
相关文章: