简体   繁体   English

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

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

Ok so first let me explain what I am doing:好的,首先让我解释一下我在做什么:

I am trying to connect to http://www.nfl.com/liveupdate/scorestrip/ss.xml to grab the xml and parse it of course cross domain policy wont allow me to do this directly.我正在尝试连接到http://www.nfl.com/liveupdate/scorestrip/ss.xml以获取 xml 并允许我直接跨域解析它。 SOOOO..太棒了。。

I am using PHP to connect to the site through a proxy and this works perfectly我正在使用 PHP 通过代理连接到站点,这非常有效

Then back in my main HTML file I am using Ajax to parse through that XML file.然后回到我的主 HTML 文件中,我正在使用 Ajax 来解析该 XML 文件。 Problem is I am getting mixed results.问题是我得到的结果好坏参半。 For instance on my macbook pro with all the latest browsers (Safari, Firefox, Chrome) this doesn't work.例如,在我的 macbook pro 上使用所有最新的浏览器(Safari、Firefox、Chrome)这不起作用。 On my iPhone it works.在我的 iPhone 上它可以工作。 and on my Mac Desktop with all latest browsers it works.并在我的 Mac 桌面上使用所有最新的浏览器。

Anyone know why?有谁知道为什么?

ALSO I have no clue what I am doing with XML this is my very first attempt to learn how to read through XML.另外,我不知道我在用 XML 做什么,这是我第一次尝试学习如何阅读 XML。 SO I may need you to explain how to better parse though as the way I am doing it now is from a fellow online user.所以我可能需要你解释如何更好地解析,因为我现在这样做的方式是来自一位在线用户。

Here is the PHP proxy which works:这是有效的 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>

Here is the troublesome HTML file with AJAX:这是带有 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>

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

I am really looking for a way to parse this as it will be an awesome learning experience.我真的在寻找一种方法来解析它,因为这将是一次很棒的学习体验。 BTW if it helps Firefox on my macbook with all the problems is telling me: missing ) in parenthetical line 12顺便说一句,如果它在我的 macbook 上对 Firefox 有帮助,所有问题都告诉我:括号中的第 12 行缺少)

Also I would greatly appreciate it if you were so kind to answer in terms a noobie may understand for dealing with XML as I am new to it.另外,如果您能以菜鸟可能理解的方式回答我,我将不胜感激,因为我是新手。

Thanks!谢谢!

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

If it's not caused by some C&P issue, this could be the cause:如果它不是由某些 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