AJax response
1. Ordinary text (character string)
resp.getWriter () Print ( "Hello");
//2.JSON give a set of front page format when using plain text transmission or transmission String object when it is time to introduce JSON
background servlet code
Student stu = new Student (16, " fifty-five", 55); // need to import-2.2.4.jar jar package GSON Gson GSON new new Gson = (); String S = gson.toJson (STU); // { "id": 16, "name ": " fifty-five", "age": 55} is converted to this format System.out.println (S); . resp.getWriter () Print (S);
Reception Code
text = xhr.responseText var; // passed in the console.log (text) the console.log (typeof text) // String Type
"{" id ": 16, " name ":" fifty-five "," Age ": 55}"
// still be converted to JSON type method using JSON
var stu= JSON.parse(text);
console.log(stu.id)
console.log(typeof stu) //object类型
[3] XML format (Learn)
var = xhr.responseXML DOC;
var name = doc.getElementsByTagName ( "name") [0] .innerHTML;