使用jQuery导入XML:在服务器上有效,而不是在本地 [英] Import XML with jQuery: works on server, not locally

查看:79
本文介绍了使用jQuery导入XML:在服务器上有效,而不是在本地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个脚本,该脚本读取XML文件,然后输出数据.当它在我的Web服务器上运行时,它可以完美运行,但不能在本地计算机上运行. ("542Data.xml"文件存储在服务器和计算机上与HTML页面相同的文件夹中,并且我检查了所有文件的版本是否相同.我在Firefox和Chrome中尝试了相同的结果)

I'm working on a script that reads an XML file and then outputs the data. It works perfectly when it runs on my web server, but won't run from my local machine. (The "542Data.xml" file is stored in the same folder as the HTML page on both the server and my computer, and I checked that all file versions are the same. I've tried it in Firefox and Chrome with the same results.)

<div id="output"></div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<script>
$(document).ready(function()
{
    $.ajax({
        type: "GET",
        url: "542Data.xml",
        dataType: "xml",
        success: parseXml
    });
});

function parseXml(xml)
{
    $(xml).find("point").each(function(index)
    {
        $("#output").append("Name: " + $(this).attr("name") + "<br />");
    });
}

</script>

XML的结构为:

<?xml version="1.0"?>
<destinations>
    <point name="Tot Lot at Bryan Park"> 
        <lat>39.15611</lat>
        <long>-86.52664</long>
        <type>outdoors</type>
    </point>
    <point name="Playground at Cascades Park"> 
        <lat>39.19633</lat>
        <long>-86.53581</long>
        <type>outdoors</type>
    </point>
</destinations>

要使它在本地运行,我需要更改什么?

What do I need to change to get this working locally?

编辑:我错了,它正在Firefox中运行. (尴尬!)

I was wrong, it's working in Firefox. (embarrassed!)

推荐答案

您的脚本在Firefox中对我来说效果很好.

Your script works fine for me in Firefox.

Chrome的某些安全功能不允许您执行想要的操作(对于AJAX请求,请使用file:///).您需要使用以下命令启动浏览器:

Chrome has some security feature that disallows what you wanted to do (using file:/// for AJAX requests). You need to start your browser with:

chrome --disable-web-security

禁用安全检查. (--allow-file-access-from-files可能也可以解决问题,但我尚未对其进行测试)

to disable security checks. (--allow-file-access-from-files might also do the trick, but I haven't tested it yet)

警告::禁用安全检查会影响您的浏览器安全性,并且仅应用于临时开发目的.如果您打算长时间在本地计算机上运行代码,请考虑在本地计算机上安装Web服务器.

Warning: disabling security checks affects your browser security and should only be used for temporary development purposes. If you plan to run the code from your local machine in a prolonged period of time, consider installing a web server on your local machine.

这篇关于使用jQuery导入XML:在服务器上有效,而不是在本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆