单击下一个和上一个按钮后,我必须使用 AJAX 调用来获取 Json 数据。 Json 数据应包含单击下一个或上一个后显示的博客内容。我的函数应该是什么样子有什么建议吗?到目前为止我只有:
function getPrev() {
$.ajax({
type: "GET",
url: "../Content/test.txt",
dataType: "json"
}).success(function (data) {
$.each(data, function (key, val) {
$('#blogcont').append(key+ val);
});
return false;
});
}
而我的 Json 文件只是一个测试文件:
{"one":"test1", "two":"test2", "three":"test3" }
对不起,我是初学者!!!! 谢谢
最佳答案
您的 $.ajax()
语法不正确
function getPrev() {
$.ajax({
type: "GET",
url: "../Content/test.txt",
dataType: "json",
success: function(data) {
var content = "";
$.each(data, function(key, val) {
content += "<p>" + key + ":" + val + "</p>";
});
$('#blogcont').html(content);
}
});
return false;
}
或
function getPrev() {
$.ajax({
type: "GET",
url: "../Content/test.txt",
dataType: "json"
}).done(function(data) {
var content = "";
$.each(data, function(key, val) {
content += "<p>" + key + ":" + val + "</p>";
});
$('#blogcont').html(content);
});
return false;
}
试试这个
function getPrev() {
$.ajax({
type: "GET",
url: "../Content/test.txt",
dataType: "json"
}).done(function(data) {
var content = "";
$.each(data, function(key, val) {
content += '<p>' + key + ':' + val + '</p>';
});
$('#blogcont').html(content)
.find('p')
.hide()
.first().show();
$('button.next').click(function() {
$('#blogcont').find('p:visible')
.hide()
.next('p').show();
});
});
return false;
}
关于javascript - 下一个和上一个按钮中的 Json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22095720/