XHR 未加载 XML 本地文件
XHR not loading a XML local file
我正在尝试将 XML 文件加载到 HTML 文档 here
这是我的脚本:
$(document).ready(function () {
$.ajax({
type: "GET",
url: "Lemon_Bars.xml",
dataType: "xml",
success: xmlParser
});
});
function xmlParser(xml) {
$(xml).find("ing").each(function () {
$("div[data-role='content']").append("<p>" + $(this).find("qty").text() + "</p>");
$("div[data-role='content']").append("<p>" + $(this).find("unit").text() + "</p>");
$("div[data-role='content']").append("<p>" + $(this).find("item").text() + "</p>");
});
}
这里是示例 XML:
<?xml version="1.0" encoding="UTF-8"?>
<recipeml version="0.5">
<recipe>
<head>
<title>#1 Lemon Bars</title>
<categories>
<cat>Desserts</cat></categories>
<yield>1</yield></head>
<ingredients>
<ing>
<amt>
<qty>1 1/2</qty>
<unit>cups</unit></amt>
<item>All-purpose flour</item></ing>
<ing>
<amt>
<qty>2/3</qty>
<unit>cups</unit></amt>
<item>Powdered sugar</item></ing>
<ing>
<amt>
<qty>1 1/2</qty>
<unit/></amt>
<item>Sticks butter; softened</item></ing>
<ing>
<amt>
<qty>3</qty>
<unit/></amt>
<item>Eggs; lightly beaten</item></ing>
<ing>
<amt>
<qty>1 1/2</qty>
<unit>cups</unit></amt>
<item>Granulated sugar</item></ing>
<ing>
<amt>
<qty>3</qty>
<unit>tablespoons</unit></amt>
<item>Flour</item></ing>
<ing>
<amt>
<qty>1/4</qty>
<unit>cups</unit></amt>
<item>Lemon juice</item></ing>
<ing>
<amt>
<qty/>
<unit/></amt>
<item>Additional powdered sugar</item></ing></ingredients>
<directions>
<step> In bowl combine flour, sugar and butter. Pat into 9X13 pan. Bake for 20
minutes in a 350F oven. While it is baking, whisk together eggs, 3 TB
flour, sugar and lemon juice. Pour over crust while crust is still hot.
Bake for an additional 20-25 minutes, or until top is golden brown. Cool on
a wire rack and dust with additional powdered sugar.
Posted to Bakery-Shoppe Digest V1 #206 by novmom@juno.com (Angela
Gilliland) on Aug 29, 1997
</step></directions></recipe></recipeml>
我得到
XMLHttpRequest cannot load file:///C:/Users/.../Lemon_Bars.xml. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
我知道我无法从另一个域加载某些内容,但为什么我无法访问同一文件夹中的文档?我知道我可以使用 JSONP,但是有没有什么方法可以让它工作,或者它现在真的被弃用了吗(帖子和教程已有 4-6 年历史了)。
如果它真的不再起作用,使用 JSONP 的正确方法是什么?
已解决。
Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
我不知道那到底是什么意思,但我的假设是,我应该通过 http 加载文件,所以我开始 XAMPP 并加载了文件并且成功了。
我正在尝试将 XML 文件加载到 HTML 文档 here
这是我的脚本:
$(document).ready(function () {
$.ajax({
type: "GET",
url: "Lemon_Bars.xml",
dataType: "xml",
success: xmlParser
});
});
function xmlParser(xml) {
$(xml).find("ing").each(function () {
$("div[data-role='content']").append("<p>" + $(this).find("qty").text() + "</p>");
$("div[data-role='content']").append("<p>" + $(this).find("unit").text() + "</p>");
$("div[data-role='content']").append("<p>" + $(this).find("item").text() + "</p>");
});
}
这里是示例 XML:
<?xml version="1.0" encoding="UTF-8"?>
<recipeml version="0.5">
<recipe>
<head>
<title>#1 Lemon Bars</title>
<categories>
<cat>Desserts</cat></categories>
<yield>1</yield></head>
<ingredients>
<ing>
<amt>
<qty>1 1/2</qty>
<unit>cups</unit></amt>
<item>All-purpose flour</item></ing>
<ing>
<amt>
<qty>2/3</qty>
<unit>cups</unit></amt>
<item>Powdered sugar</item></ing>
<ing>
<amt>
<qty>1 1/2</qty>
<unit/></amt>
<item>Sticks butter; softened</item></ing>
<ing>
<amt>
<qty>3</qty>
<unit/></amt>
<item>Eggs; lightly beaten</item></ing>
<ing>
<amt>
<qty>1 1/2</qty>
<unit>cups</unit></amt>
<item>Granulated sugar</item></ing>
<ing>
<amt>
<qty>3</qty>
<unit>tablespoons</unit></amt>
<item>Flour</item></ing>
<ing>
<amt>
<qty>1/4</qty>
<unit>cups</unit></amt>
<item>Lemon juice</item></ing>
<ing>
<amt>
<qty/>
<unit/></amt>
<item>Additional powdered sugar</item></ing></ingredients>
<directions>
<step> In bowl combine flour, sugar and butter. Pat into 9X13 pan. Bake for 20
minutes in a 350F oven. While it is baking, whisk together eggs, 3 TB
flour, sugar and lemon juice. Pour over crust while crust is still hot.
Bake for an additional 20-25 minutes, or until top is golden brown. Cool on
a wire rack and dust with additional powdered sugar.
Posted to Bakery-Shoppe Digest V1 #206 by novmom@juno.com (Angela
Gilliland) on Aug 29, 1997
</step></directions></recipe></recipeml>
我得到
XMLHttpRequest cannot load file:///C:/Users/.../Lemon_Bars.xml. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
我知道我无法从另一个域加载某些内容,但为什么我无法访问同一文件夹中的文档?我知道我可以使用 JSONP,但是有没有什么方法可以让它工作,或者它现在真的被弃用了吗(帖子和教程已有 4-6 年历史了)。
如果它真的不再起作用,使用 JSONP 的正确方法是什么?
已解决。
Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
我不知道那到底是什么意思,但我的假设是,我应该通过 http 加载文件,所以我开始 XAMPP 并加载了文件并且成功了。