【问题标题】:get html markup in xml json response - jquery ajax在 xml json 响应中获取 html 标记 - jquery ajax
【发布时间】:2014-10-18 15:15:13
【问题描述】:

我有一个名为 InnerHtml 的 xml 标记,其中包含 html 字段的标记。

C#

Result = "<?xml version='1.0' encoding='UTF-8'?><GETRESPONSE><FIELDS><FIELD><LABEL>FIRST NAME</LABEL><INNERHTML><div class='form-group'><label>First Name</label><input  id='txtFirstName' type='text' pattern='[0-9]{9,9}' class='large' /><label id='Error-FirstName' class='error'></label></div></INNERHTML></FIELD></FIELDS></GETRESPONSE>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(Result);
Result = JsonConvert.SerializeXmlNode(doc);

我正在进行 jquery ajax 调用并返回上述响应。现在我想从上面的 XML/JSON 响应中提取 InnerHtml 并附加到我现有的 html 标记

Jquery Ajax 调用

success: function (data) {
var i;
var html;
var fieldLength = data.GETBILLERDETAILSRESPONSE.FIELDS.FIELD.length;
for (i = 0; i < fieldLength; i++) {
    console.log(JSON.stringify(data.GETRESPONSE.FIELDS.FIELD[i].INNERHTML));
}
}

我在控制台中得到的是

{"div":{"@class":"form-group","label":["First Bane",{"@id":"Error-FirstName","@class":"error"}],"input":{"@id":"txtFirstName","@type":"text","@pattern":"[0-9]{9,9}","@class":"large"}}}

但我想要清晰的标记而不是上面

【问题讨论】:

  • 我认为您可以传递 XML 字符串而不是 XMLDocument,并且在客户端您可以使用 XML 解析将字符串转换为 XML 文档。

标签: c# javascript jquery html ajax


【解决方案1】:

尝试将 html 放在 CDATA 中,像这样

Result = "<?xml version='1.0' encoding='UTF-8'?><GETRESPONSE><FIELDS><FIELD><LABEL>FIRST NAME</LABEL><INNERHTML><![CDATA[<div class='form-group'><label>First Name</label><input  id='txtFirstName' type='text' pattern='[0-9]{9,9}' class='large' /><label id='Error-FirstName' class='error'></label></div>]]></INNERHTML></FIELD></FIELDS></GETRESPONSE>";

【讨论】:

  • 它在 js 中的输出为{"#cdata-section":"&lt;div class='form-group'&gt;&lt;label&gt;CA Number&lt;/label&gt;&lt;input id='Ggcl-CANumber' type='text' pattern='[0-9]{9,9}' class='large' /&gt;&lt;label id='Error-Ggcl-CANumber' class='error'&gt;&lt;/label&gt;&lt;/div&gt;"} 如何只获取变量中的div 部分?
  • console.log(JSON.stringify(data.GETRESPONSE.FIELDS.FIELD[i].INNERHTML['#cdata-section']));
  • 你确定吗?也许你的 javascript 被缓存了?
  • 尝试清除缓存,然后再次运行。确保您的浏览器没有缓存您的 js(可能是作为测试的警报?)。
  • 已解决。 JSON.stringify 不需要
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-04
  • 1970-01-01
  • 2017-08-30
  • 2011-05-12
  • 1970-01-01
  • 2012-10-25
相关资源
最近更新 更多