简体   繁体   English

使用jQuery导入XML:可在服务器上使用,而不能在本地使用

[英]Import XML with jQuery: works on server, not locally

I'm working on a script that reads an XML file and then outputs the data. 我正在处理一个脚本,该脚本读取XML文件,然后输出数据。 It works perfectly when it runs on my web server, but won't run from my local machine. 当它在我的Web服务器上运行时,它可以完美运行,但不能在本地计算机上运行。 (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.) (“ 542Data.xml”文件存储在服务器和计算机上与HTML页面相同的文件夹中,并且我检查了所有文件的版本是否相同。我在Firefox和Chrome中尝试使用了相同的结果)

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

The XML is structured as: 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? 我需要更改什么才能使它在本地工作?

EDIT: I was wrong, it's working in Firefox. 编辑:我错了,它在Firefox中工作。 (embarrassed!) (尴尬!)

Your script works fine for me in Firefox. 您的脚本在Firefox中对我来说效果很好。

Chrome has some security feature that disallows what you wanted to do (using file:/// for AJAX requests). Chrome浏览器具有某些安全功能,该功能不允许您执行操作(使用file:///进行AJAX请求)。 You need to start your browser with: 您需要使用以下命令启动浏览器:

chrome --disable-web-security

to disable security checks. 禁用安全检查。 ( --allow-file-access-from-files might also do the trick, but I haven't tested it yet) --allow-file-access-from-files也可以解决这个问题,但我尚未对其进行测试)

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. 如果您打算长时间在本地计算机上运行代码,请考虑在本地计算机上安装Web服务器。

If by "working locally" you mean you have the html and xml file in a folder and open the HTML file by double clicking on it, then there is no way. 如果“本地工作”是指您将html和xml文件放在一个文件夹中,然后双击该文件来打开HTML文件,那么就没有办法。

In order for it to work locally you need an web server which will resolve http requests. 为了使其在本地工作,您需要一个Web服务器来解析http请求。 Opening a local file on a file system is not what is happening here. 在文件系统上打开本地文件不是这里发生的事情。 .ajax() is making a server request. .ajax()正在发出服务器请求。 Without a server it won't work. 没有服务器,它将无法正常工作。

What are you using to develop? 您正在使用什么来发展? Check if the development server you are using can serve XML files. 检查您使用的开发服务器是否可以提供XML文件。

According to the given (small) info. 根据给定的(小)信息。 There is may be a security reason ie importing jquery from google's repository. 可能出于安全原因,即从Google的存储库导入jquery。 Please give more code or look at the error console in firefox - ctr+shift+j and copy paste the error if there is any, or just download jquery and include it with path in local location. 请提供更多代码或查看firefox中的错误控制台-ctr + shift + j,如果有错误,请复制并粘贴错误,或者直接下载jquery并将其包含在本地路径中。

It is running on server but not on your machine. 它正在服务器上运行,但不在您的计算机上运行。 See, the ajax request needs a local server running. 看到,ajax请求需要本地服务器运行。 To make it work, start some local server on your machine. 要使其工作,请在计算机上启动一些本地服务器。 For example, if you're on Windows, then download WAMP, and if on Linux, then install LAMP. 例如,如果您使用Windows,则下载WAMP;如果使用Linux,则安装LAMP。 Put your files in www folder. 将您的文件放在www文件夹中。 Then start the local server..and then access your file using localhost/your_file_name. 然后启动本地服务器..然后使用localhost / your_file_name访问您的文件。 That'll give you the result as you want it. 这将为您提供所需的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM