Mybutton.addeventlistener (' click ', (e) = ={Let request=NewXMLHttpRequest () Request.onreadystatechange= () + = { if(Request.readystate = = 4) {Console.log (' Request complete ') if(Request.status >= & Request.status <= 300) {Console.log (' Request succeeded ') Console.log (request.responsetext)//Parsing XML //Let parser = new Domparser (); //Let xmldoc = parser.parsefromstring (Request.responsetext, "Text/xml") //Let body=xmldoc.getelementsbytagname (' body ') [0].textcontent //Console.log (body) //parsing the JSON server returns a stringLet string =request.responsetext Let obj=window. Json.parse (String) console.log (obj) console.log (obj.note) Console.log (obj.not e.to)}Else if(Request.status >= 400) {Console.log (' Request failed ')}}} Request.open (' GET ', '/xxx ') Request.send ()})
Sending Ajax requests using native JS (Xml,json parsing)