English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Ajax Receiving Background Data and Displaying It on HTML Page

Java code

 PrintWriter out=response.getWriter(); //Send character data to the client
 response.setContentType("text/text"); //Set the content type and encoding method of the request and response
 response.setCharacterEncoding("UTF-8");
 JSONArray json = JSONArray.fromObject(newsList); //Convert the newsList object to a json object
 String str = json.toString(); //Convert the json object to a string
 out.write(str); //Transfer the str character to the front-end 
 

Ajax code

 $(document).ready(function() {
 $.ajax({
 url : "newsservlet",//Request address
 dataType : "json",//Data format 
 type : "post",//Request method
 async : false,//Is the request asynchronous
 success : function(data) { //How to send successfully
 var html = "";
 for(var i=0;i<data.length;i++{ //Traverse the data array
 var ls = data[i]; 
 html +="<li><a href='second page text.html#63;newsid="+ls.news_id+"'class='infNews_word_a'><span>"+ls.news_name+"</span></a><span class='date'>"+ls.news_time+"</span></li>";
 }
 $("#ulul").html(html); //Display HTML content in the tag with id ulul in the html page
 },
>)
>)

HTML page

<ul id="ulul"></ul>

In AJAX, "#" represents the id of a tag, and "." represents the class of a tag.

In the Java background, setting the content type and encoding method of the request and response must be written before the json object is converted to a string, otherwise it will cause乱码 in json Chinese.

That's all for this article. I hope the content of this article can bring you some help in learning or work, and I also hope to support the Shouting Tutorial more!

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume relevant legal liabilities. If you find any content suspected of copyright infringement, please send an email to notice#w.3Please send an email to codebox.com (replace # with @ when sending email) to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You May Also Like