2

我正在对一个服务进行 ajax 调用,该服务使用此代码将 html 作为字符串返回..

var xhReq = new XMLHttpRequest();
xhReq.open("POST", "MobileClientService.svc/REST/TestHtmlSend", false);
xhReq.send(null);
xhReq.responseType = "text/html";
var serverResponse = xhReq.responseText;
alert(serverResponse); // Shows "15"

该服务正确创建了html..

<div data-bb-type="item" data-bb-img="Images/imagesBBUI/icons/icon11.png" data-bb-title="Title From Server">

</div>

问题是我的代码中的“serverResponse”正在返回这个..

<div data-bb-type=\"item\" data-bb-img=\"Images/imagesBBUI/icons/icon11.png\" data-bb-title=\"Title From Server\">
  \u000d\u000a\u000d\u000a
<\/div>

这是用于创建 html 的 C# 代码。

  public string TestHtmlSend()
        {
            string moomoo = String.Empty;

            // Initialize StringWriter instance.
            StringWriter stringWriter = new StringWriter();

            // Put HtmlTextWriter in using block because it needs to call Dispose.
            using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
            {
                //start collapse div
                writer.AddAttribute("data-bb-type", "item");
                writer.AddAttribute("data-bb-img", "Images/imagesBBUI/icons/icon11.png");
                writer.AddAttribute("data-bb-title", "Title From Server");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                //writer.Write("Some Whatever Description");
                writer.RenderEndTag();

            }

            moomoo = stringWriter.ToString();

如何更改我的代码以返回没有所有额外“\”的html?

4

1 回答 1

1

我以前遇到过这样的奇怪问题,通常归结为中间件。

看起来像服务器端的变量插值问题。

您的中间件语言和/或框架是什么?

确保以干净的方式打印到输出流:

  • 仅使用单个变量(您可以轻松地看到报价插值)
  • 不要不必要地逃避你的输出
于 2012-09-28T20:29:11.450 回答