如何将jquery ajax数据提取为html
How to extract jquery ajax data to html?
我很难理解为什么它不起作用。我的.json文件是正确的,但我无法提取数据以在Html上显示它们。
weather.js:
$(document).ready(function() {
$.ajax({
url: "weather.json",
type: "GET",
dataType: "json", // Returns JSON
success: function(response) {
var sTxt = '';
$.each(response.weather, function(index) {
sTxt += '<tr><td>' + response.weather[index].name + '</td></tr>';
});
$('#weatherlist').append(sTxt);
},
error: function() {
$('#info').html('<p>An error has occurred</p>');
}
});
});
weather.html:
<!DOCTYPE html>
<html>
<head>
<title>Ajax and json Data</title>
<meta charset="UTF-8">
<script src="weather.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<header>
<h1>British and Irish Counties - Weather Data</h1>
</header>
<section>
<table id="weatherlist">
</table>
<div id="updatemessage"></div>
</section>
<footer>
</footer>
</body>
</html>
这是新的Html,与之前的现有Html相比,我仍然有一个错误:jquery.min.js:4XMLHttpRequest无法加载file:///C:/Users/jull/Desktop/weather.json.跨源请求仅支持以下协议方案:http、data、chrome、chrome扩展、https、chrome资源扩展。
<!DOCTYPE html>
<html>
<head>
<title>Ajax and json Data</title>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="weather.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<header>
<h1>British and Irish Counties - Weather Data</h1>
</header>
<section>
<table id="weatherlist">
</table>
<div id="updatemessage"></div>
</section>
<footer>
</footer>
</body>
</html>
打开浏览器的开发工具。查看控制台。它会说:
未捕获的ReferenceError:$未定义
您正在尝试使用jQuery,但尚未包含它。
添加另一个脚本元素(在您已经添加的脚本元素之前),并使用它来加载jQuery库。
You have to use
JSON.parse(response)
相关文章:
- 用于提取 HTML 标记子元素的正则表达式
- 如何使用mozilla插件sdk API从响应中提取html页面
- 从字符串中提取HTML标记并“使用”它
- 使用 jQuery 提取 HTML 标记之外的文本
- 从动态页面中提取 html 源代码
- 提取 html 页面的页眉和页脚
- 提取 html 元素之间的文本
- 用于提取 HTML 标记的 XPaPath
- 如何使用 jQuery 从使用 Ajax 获取的内容中提取 HTML
- 使用javascript提取HTML
- 提取html页面中所有以.js扩展名结尾的链接
- 从Javascript关联数组中提取HTML链接以应用于D3生成的表
- 使用PhantomJS提取html和文本
- 使用javascript/jquery从字符串中提取html元素
- 如何提取html并将其添加到数组中
- 如何从纯文本中提取html标记
- UIWebView-如何从Facebook等网页中提取HTML代码
- 如何使用javascript或jquery从字符串中提取html标记的内容
- 正确提取HTML
- regex来提取html注释属性