使用jQuery解析国家威胁级XML文件

时间:2021-07-09 09:28:36

I'm trying to use jQuery to parse the DHS Threat Level that they have in their XML file, but I can't seem to get it to work. I'm very new with Javascript and non-presentational jQuery, so I'm probably missing something obvious.

我正在尝试使用jQuery解析他们在XML文件中的DHS威胁级别,但我似乎无法让它工作。我是Javascript和非演示jQuery的新手,所以我可能错过了一些明显的东西。

This is the XML file:

这是XML文件:

<?xml version="1.0" encoding="UTF-8" ?> 
<THREAT_ADVISORY CONDITION="ELEVATED" /> 

This is the Javascript I've written:

这是我写的Javascript:

$(document).ready(function() {
    $.ajax({
        type     :  'GET',
        url      :  'http://www.dhs.gov/dhspublic/getAdvisoryCondition',
        dataType :  'xml',
        success  :  function parseThreatLevel(xml) {
                        var threatLevel = $(xml).find('threat_advisory').attr('condition');
                        $('#nationalThreatLevel').append(threatLevel);
                    }
    });
});

Below is the HTML I'm trying to use. Thanks in advance for any help or advice!

下面是我正在尝试使用的HTML。在此先感谢您的任何帮助或建议!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'></script>
    <script type='text/javascript' src='lib/parseThreatLevel.js'></script>
        <title>National Threat Level</title>
    </head>
    <body>
        <div id='nationalThreatLevel'></div>
    </body>
</html>

3 个解决方案

#1


For security reasons, javascript has no access to other domains. To bypass this, you need to add a script on your server that does have access to other domains - often times called a proxy.

出于安全原因,javascript无法访问其他域。要绕过此操作,您需要在服务器上添加一个可以访问其他域的脚本 - 通常称为代理。

PHP is a great option for many web-developers. What you would do is post the website url you want to hit to your local PHP script, it will then request that page and return the contents of it to your jQuery script which you can then parse for the data you desire.

PHP是许多Web开发人员的绝佳选择。您要做的是将您要点击的网站网址发布到您当地的PHP脚本,然后它将请求该页面并将其内容返回到您的jQuery脚本,然后您可以解析所需的数据。

Non-SO Solutions:

* Archive:

#2


All of your code is correct, but the problem is that you are making a cross domain request. The only way this will work with JavaScript / jQuery is if you can find a JSONP provider. Well, that or you write your own server side (PHP/ASP/ColdFusion/Something) to retrieve the XML and provide a local copy to be able to retrieve the XML using JavaScript.

您的所有代码都是正确的,但问题是您正在发出跨域请求。如果您可以找到JSONP提供程序,那么这将适用于JavaScript / jQuery的唯一方法。那么,或者您编写自己的服务器端(PHP / ASP / ColdFusion / Something)来检索XML并提供本地副本以便能够使用JavaScript检索XML。

#3


XML is case-sensitive, isn't it? You might try using THREAT_ADVISORY and CONDITION. That is,

XML区分大小写,不是吗?您可以尝试使用THREAT_ADVISORY和CONDITION。那是,

var threatLevel = $(xml).find('THREAT_ADVISORY').attr('CONDITION');

#1


For security reasons, javascript has no access to other domains. To bypass this, you need to add a script on your server that does have access to other domains - often times called a proxy.

出于安全原因,javascript无法访问其他域。要绕过此操作,您需要在服务器上添加一个可以访问其他域的脚本 - 通常称为代理。

PHP is a great option for many web-developers. What you would do is post the website url you want to hit to your local PHP script, it will then request that page and return the contents of it to your jQuery script which you can then parse for the data you desire.

PHP是许多Web开发人员的绝佳选择。您要做的是将您要点击的网站网址发布到您当地的PHP脚本,然后它将请求该页面并将其内容返回到您的jQuery脚本,然后您可以解析所需的数据。

Non-SO Solutions:

* Archive:

#2


All of your code is correct, but the problem is that you are making a cross domain request. The only way this will work with JavaScript / jQuery is if you can find a JSONP provider. Well, that or you write your own server side (PHP/ASP/ColdFusion/Something) to retrieve the XML and provide a local copy to be able to retrieve the XML using JavaScript.

您的所有代码都是正确的,但问题是您正在发出跨域请求。如果您可以找到JSONP提供程序,那么这将适用于JavaScript / jQuery的唯一方法。那么,或者您编写自己的服务器端(PHP / ASP / ColdFusion / Something)来检索XML并提供本地副本以便能够使用JavaScript检索XML。

#3


XML is case-sensitive, isn't it? You might try using THREAT_ADVISORY and CONDITION. That is,

XML区分大小写,不是吗?您可以尝试使用THREAT_ADVISORY和CONDITION。那是,

var threatLevel = $(xml).find('THREAT_ADVISORY').attr('CONDITION');