簡體   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