使用Javascript解析HTML中的AJAX resposne [英] Parse AJAX resposne in HTML using Javascript
问题描述
function loadFacility(callback)
{
// alert('in loadFacility');
var xmlhttp;
var keys = document.firstCallInformation.facilityselect.value;
var urls =http:// localhost:8080 / webfdms / showFirstCallInformation.do?vitalsId = 366;
if(window.XMLHttpRequest)
{//代码为IE7 +,Firefox,Chrome,Opera,Safari
xmlhttp = new XMLHttpRequest();
}
else
{//代码为IE6,IE5
xmlhttp = new ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4&& xmlhttp.status == 200)
{
// var some = xmlhttp.responseXML.documentElement;
var response = xmlhttp.responseText;
console.log(response)
callback(xmlhttp.responseText);
}
}
xmlhttp.open(GET,url,true);
xmlhttp.send(null);
function loadFacilityCallback(response){
if(response!= null){
// alert(response);
console.log(response);
var div = document.createElement(div);
div.innerHTML = response;
document.getElementById(facilityselect)。innerHTML = div.querySelectorAll(select#facilityselect);;
编辑:
我已更新我的回调函数。但是在这里,我收到了选择列表作为[Object Nodelist]。现在我怎么能显示在我的HTML?
在回调函数中,我现在收到响应为HTML,我想解析该HTML响应,以便我可以进一步处理它。我使用普通的JavaScript来做到这一点。请任何人告诉我如何解析接收到的HTML格式的ajax响应。 创建一个 DIV code>元素并将其放入
innerHTML
中。这将解析它。
var div = document.createElement(div);
div.innerHTML = response;
现在您可以在 div
中处理它,例如 div.querySelector( 类名)
。要获取所有< select>
标签,请执行以下操作:
var选择= div.querySelectorAll(选择);
要放入您的网页,您可以:
document.getElementById(facilityselect)。innerHTML = div.querySelector(select#facilityselect)。innerHTML
I am using AJAX call in my code using Javascript.
function loadFacility(callback)
{
//alert('In loadFacility');
var xmlhttp;
var keys=document.firstCallInformation.facilityselect.value;
var urls="http://localhost:8080/webfdms/showFirstCallInformation.do?vitalsId=366";
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status == 200)
{
//var some=xmlhttp.responseXML.documentElement;
var response = xmlhttp.responseText;
console.log(response)
callback(xmlhttp.responseText);
}
}
xmlhttp.open("GET",urls,true);
xmlhttp.send(null);
}
function loadFacilityCallback(response){
if(response != null){
//alert(response);
console.log(response);
var div = document.createElement("div");
div.innerHTML = response;
document.getElementById("facilityselect").innerHTML = div.querySelectorAll("select#facilityselect");;
}
EDIT: I have updated my callback function. But here I received select list as [Object Nodelist]. Now how can I display in my HTML ?
In callback function I received the response as HTML now I want to parse that HTML response so that I can process it further. I am using plain javascript to do so. Please any one tell me how to parse ajax response received as HTML.
Create a DIV
element and put the HTML in it innerHTML
. That will parse it.
var div = document.createElement("div");
div.innerHTML = response;
Now you can process it in div
, e.g. div.querySelector(".classname")
. To get all the <select>
tags, do:
var selects = div.querySelectorAll("select");
To put it into your webpage, you can do:
document.getElementById("facilityselect").innerHTML = div.querySelector("select#facilityselect").innerHTML
这篇关于使用Javascript解析HTML中的AJAX resposne的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!