gpt4 book ai didi

javascript - 使用 Ajax 从特定 URL 加载数据

转载 作者:行者123 更新时间:2023-12-03 10:21:46 24 4
gpt4 key购买 nike

我对 javascript 和 ajax 很陌生,并且在使用正确的 javascript 代码在 HTML 文档中显示数据时遇到了一些严重的问题。我需要将特定网址的数据显示在 HTML 文档上。谁能指出我正确的方向?

application.js

function load_test(list_item) {
var list = list_item.find('dl');
var promise = $.ajax('http://www.ehustudent.co.uk/hallm/cis1007api/student-feedback/test' + list_item.data('id'));
promise.done(function(data) {
for(var idx = 0; idx < data.length; idx++) {
list.append('<dt>' + data[idx].id + '</dt>');
list.append('<dd>' + data[idx].studentnr + '</dd>');
list.append('<dd>' + data[idx].name + '</dd>');
list.append('<dd>' + data[idx].feedback + '</dd>');
}
});
}

index.html

<html>
<head>
<link rel="stylesheet" href="style.css"/>
<script src="jquery.min.js"></script>
<script src="application.js"></script>
</head>
<body>
<div class="row">
<div class="column small-12">
<h1>Feedback</h2>
</div>
</div>
<ol class="feedback">
<li class="row">
<div class="column small-12 large-10">
<h2>Student Name Goes Here</h2>
</div>
<div class="column small-12 large-2 text-right">
<a href="#">(delete)</a>
</div>
<div class="column small-12">
<p>Student Feedback Goes Here</p>
</div>
</li>
</ol>
</ol>
<form id="add_feedback" action="#">
<div class="row">
<div class="column small-12 large-6">
<label>Name
<input type="text" name="name" placeholder="Enter your name here"/>
</label>
</div>
</div>
<div class="row">
<div class="column small-12 large-6">
<label>Feedback
<textarea name="feedback" placeholder="Please provide some useful and constructive feedback"></textarea>
</label>
</div>
</div>
<div class="row">
<div class="column small-12 large-6 text-right">
<input type="submit" value="Add Feedback"/>
</div>
</div>
</form>

任何帮助将不胜感激。谢谢!

最佳答案

试试这个代码:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled 1</title>
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
function load_test(list_item) {
var list = list_item.find('li');
var promise = $.ajax('http://www.ehustudent.co.uk/hallm/cis1007api/student-feedback/test' );
promise.done(function(data) {
console.log(data);
for(var idx = 0; idx < data.length; idx++) {
list.append('<dt>' + data[idx].id + '</dt>');
list.append('<dd>' + data[idx].studentnr + '</dd>');
list.append('<dd>' + data[idx].name + '</dd>');
list.append('<dd>' + data[idx].feedback + '</dd>');
}
});
}
$(function(){
load_test($('.feedback'));
});
</script>
</head>
<body>
<div class="row">
<div class="column small-12">
<h1>Feedback</h2>
</div>
</div>
<ol class="feedback">
<li class="row">
<div class="column small-12 large-10">
<h2>Student Name Goes Here</h2>
</div>
<div class="column small-12 large-2 text-right">
<a href="#">(delete)</a>
</div>
<div class="column small-12">
<p>Student Feedback Goes Here</p>
</div>
</li>
</ol>
<form id="add_feedback" action="#">
<div class="row">
<div class="column small-12 large-6">
<label>Name
<input type="text" name="name" placeholder="Enter your name here"/>
</label>
</div>
</div>
<div class="row">
<div class="column small-12 large-6">
<label>Feedback
<textarea name="feedback" placeholder="Please provide some useful and constructive feedback"></textarea>
</label>
</div>
</div>
<div class="row">
<div class="column small-12 large-6 text-right">
<input type="button" onclick="tex" value="Add Feedback"/>
</div>
</div>
</form>
</body>
</html>

关于javascript - 使用 Ajax 从特定 URL 加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29593444/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com