英文:
How to append an AJAX reply
问题
我在网站上搜索了一下,令人惊讶的是没有找到适合我的情况的答案,所以我发布了这个问题。
我正在使用jQuery AJAX来获取一个网页,并将其动态添加到网站上。但是我的代码不起作用,抛出了一个错误“ERROR TypeError: "data.append is not a function"”。
我的代码是:
$.ajax({
url: "http://localhost:3000/treeviewuser/testuser",
beforeSend: function( xhr ) {
xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
}
})
.done(function( data ) {
if ( console && console.log ) {
console.log( "Sample of data:", data.slice( 0, 100 ) );
data.append(data);
}
});
});
英文:
I searched the site and surprisingly did not find any answers fitting my situation. So I posted this question.
I am using jQuery AJAX to fetch a web page and dynamically add it to the website. But my code does not work and throws an error ERROR TypeError: "data.append is not a function"
.
My code is:
$.ajax({
url: "http://localhost:3000/treeviewuser/testuser",
beforeSend: function( xhr ) {
xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
}
})
.done(function( data ) {
if ( console && console.log ) {
console.log( "Sample of data:", data.slice( 0, 100 ) );
data.append(data);
}
});
});
答案1
得分: 3
以下是翻译好的部分:
要进一步说明我的评论,data
是从端点返回的响应,它没有 .append()
方法。根据你的回复,你打算将输出打印到文档中。有两种方法可以做到这一点:
1. 创建一个新元素并附加到文档
如果页面上没有您想要附加数据的目标元素,您可以简单地创建一个新元素并将其内部文本设置为从服务器返回的数据:
$.ajax({
url: "http://localhost:3000/treeviewuser/testuser",
beforeSend: function( xhr ) {
xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
}
}).done(function( data ) {
const el = document.createElement('div');
el.innerText = data;
document.appendChild(el);
});
如果您更喜欢使用 jQuery 编写,您也可以这样做:
$.ajax({
url: "http://localhost:3000/treeviewuser/testuser",
beforeSend: function( xhr ) {
xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
}
}).done(function( data ) {
const $el = $('<div />', {
text: data
});
$(document).append($el);
});
2. 将文本附加到现有元素
假设您有一个带有 ID 为 output
的元素,您希望将响应打印到该元素中,即 <div id="output"></div>
,那么您可以这样做:
$.ajax({
url: "http://localhost:3000/treeviewuser/testuser",
beforeSend: function( xhr ) {
xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
}
}).done(function( data ) {
$('#output').text(data);
});
英文:
To further elabourate on my comment, data
is the response returned from the endpoint, which does not have the .append()
method. Based on your response, you intend to print the output to the document. There are two ways to do this:
##1. Create a new element with the text, and append to document
If you do not have a target element on the page you want to append the data to, you can simply create a new element and set its inner text to the data returned from the server:
$.ajax({
url: "http://localhost:3000/treeviewuser/testuser",
beforeSend: function( xhr ) {
xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
}
}).done(function( data ) {
const el = document.createElement('div');
el.innerText = data;
document.appendChild(el);
});
If you're more comfortable writing in jQuery, you can also do this:
$.ajax({
url: "http://localhost:3000/treeviewuser/testuser",
beforeSend: function( xhr ) {
xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
}
}).done(function( data ) {
const $el = $('<div />', {
text: data
});
$(document).append($el);
});
##2. Append text to a pre-existing element
Let's say you have an element with the ID of output
that you want to print the response to, i.e. <div id="output"></div>
, then you can do this:
$.ajax({
url: "http://localhost:3000/treeviewuser/testuser",
beforeSend: function( xhr ) {
xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
}
}).done(function( data ) {
$('#output').text(data);
});
答案2
得分: 1
你需要找到要附加响应数据的元素。
`$("#my_div_id").append(data[0])` 而不是 `data.append(data);`
如果你的数据是一个 _**`数组`**_ 或者 _**`对象数组`**_,在附加时需要循环遍历它们;如果来自你的 AJAX 请求的数据是 _**`文本`**_ 或 _**`HTML`**_ 类型,你可以直接将它附加到你的文档上。 `$("#my_div_id").append(data)`
英文:
You need to find the element where you want to append the response data.
$("#my_div_id").append(data[0])
instead of data.append(data);
If your data is an array
or an array of objects
, you'll need to loop through each of them when appending; else when the data you return from your ajax is in a type of text
or html
, you can just directly append it on your document. $("#my_div_id").append(data)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论