[英]Parse AJAX response in HTML using Javascript
I am using AJAX call in my code using Javascript. 我在使用Javascript的代码中使用AJAX调用。
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 ?
现在如何在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. 在回调函数中,我现在想以HTML格式接收响应,因此我想解析该HTML响应,以便进一步处理它。 I am using plain javascript to do so.
我正在使用普通的javascript来做到这一点。 How to parse ajax response received as HTML?
如何解析以HTML格式接收的Ajax响应?
Create a DIV
element and put the HTML in it innerHTML
. 创建一个
DIV
元素,并将HTML放入其中innerHTML
。 That will parse it. 那将解析它。
var div = document.createElement("div");
div.innerHTML = response;
Now you can process it in div
, eg div.querySelector(".classname")
. 现在,您可以在
div
中对其进行处理,例如div.querySelector(".classname")
。 To get all the <select>
tags, do: 要获取所有
<select>
标记,请执行以下操作:
var selects = div.querySelectorAll("select");
To put it into your webpage, you can do: 要将其放入您的网页,您可以执行以下操作:
document.getElementById("facilityselect").innerHTML = div.querySelector("select#facilityselect").innerHTML
Try this: 尝试这个:
var dom='Your http response';
var wrapper=document.createElement('div');
wrapper.innerHTML=dom;
var elemContainer=wrapper.firstChild;
var elemToFind=elemContainer.querySelectorAll('option');
var facilityselect=document.getElementById('facilityselect');
facilityselect.innerHTML='';
for(var i=0, len=elemToFind.length; i < len; i++){
facilityselect.appendChild(elemToFind[i]);
}
Creating a fake div element and appending your string which contains a HTML string will help.
创建一个伪造的div元素并附加包含HTML字符串的字符串会有所帮助。 To insert a DOM object in specific selector, you need to user appendChild method.In case you want to empty the selector and then insert the DOM then set innerHTML as empty (selector.innerHTML="") and then do the append operation.
要在特定选择器中插入DOM对象,您需要使用appendChild方法。如果要清空选择器,然后插入DOM,然后将innerHTML设置为空(selector.innerHTML =“”) ,然后执行添加操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.