如何创建和使用XMLHttpRequest对象

如何创建和使用XMLHttpRequest对象


梦里花落0921
浏览 868回答 1
1回答

撒科打诨

这里有个简单的例程:1,创建并获取XMLHttpRequest对象:function getXMLHttpRequest(){var http_request = false;if (window.XMLHttpRequest){http_request = new XMLHttpRequest();if (http_request.overrideMimeType){http_request.overrideMimeType('text/xml');}}else if (window.ActiveXObject){try{http_request = new ActiveXObject("Msxml2.XMLHTTP");}catch (e){try{http_request = new ActiveXObject("Microsoft.XMLHTTP");}catch (e){}}}if (!http_request){alert('Giving up :( Cannot create an XMLHTTP instance');return false;}return http_request;}2,使用XMLHttpRequest发送请求并处理数据:function make_request(url){http_request = getXMLHttpRequest();if (http_request){http_request.onreadystatechange = function (){//调用自己的方法(获取数据http_request.responseText)//如:alertContents(http_request);};http_request.open('GET', url, true);http_request.send(null);}}此函数为上面的方法调用:function alertContents(http_request){if (http_request.readyState == 4){if (http_request.status == 200){parse_data(http_request.responseText);}}}
打开App,查看更多内容
随时随地看视频慕课网APP