如何追加 AJAX 回复

huangapple go评论74阅读模式
英文:

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: &quot;http://localhost:3000/treeviewuser/testuser&quot;,
	beforeSend: function( xhr ) {
		xhr.overrideMimeType( &quot;text/plain; charset=x-user-defined&quot; );
	}
}).done(function( data ) {
	const el = document.createElement(&#39;div&#39;);
	el.innerText = data;

    document.appendChild(el);
});

If you're more comfortable writing in jQuery, you can also do this:

$.ajax({
	url: &quot;http://localhost:3000/treeviewuser/testuser&quot;,
	beforeSend: function( xhr ) {
		xhr.overrideMimeType( &quot;text/plain; charset=x-user-defined&quot; );
	}
}).done(function( data ) {
	const $el = $(&#39;&lt;div /&gt;&#39;, {
		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. &lt;div id=&quot;output&quot;&gt;&lt;/div&gt;, then you can do this:

$.ajax({
	url: &quot;http://localhost:3000/treeviewuser/testuser&quot;,
	beforeSend: function( xhr ) {
		xhr.overrideMimeType( &quot;text/plain; charset=x-user-defined&quot; );
	}
}).done(function( data ) {
	$(&#39;#output&#39;).text(data);
}); 

答案2

得分: 1

你需要找到要附加响应数据的元素。
`$(&quot;#my_div_id&quot;).append(data[0])` 而不是 `data.append(data);`
如果你的数据是一个 _**`数组`**_ 或者 _**`对象数组`**_,在附加时需要循环遍历它们;如果来自你的 AJAX 请求的数据是 _**`文本`**_ 或 _**`HTML`**_ 类型,你可以直接将它附加到你的文档上。 `$(&quot;#my_div_id&quot;).append(data)`
英文:

You need to find the element where you want to append the response data.
$(&quot;#my_div_id&quot;).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. $(&quot;#my_div_id&quot;).append(data)

huangapple
  • 本文由 发表于 2020年1月6日 21:07:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612691.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定