简体   繁体   English

将HTML返回XMLHttpRequest对象

[英]Returning Html To XMLHttpRequest Object

I am making an ajax call to a service that returns html as a string with this code.. 我正在对此代码返回以字符串形式返回html的服务进行ajax调用。

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"

The service creates the html correctly.. 该服务可正确创建html。

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

</div>

The problem is "serverResponse" in my code is returning this.. 问题是我的代码中的“ 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>

Here is the C# code used to create the html.. 这是用于创建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();

How do I change my code to return the html as it is without all the extra "\\"? 如何更改我的代码以按原样返回html,而没有所有额外的“ \\”?

I've run into weird issues like this before and it usually always boils down to the middleware. 我以前遇到过这样奇怪的问题,通常总是归结为中间件。

Looks like a variable interpolation issue on the server side. 看起来像服务器端的变量插值问题。

What is your middleware language and/or framework? 您的中间件语言和/或框架是什么?

Ensure that you are printing to your output stream in a clean manner: 确保以干净的方式打印到输出流:

  • using only a single variable (where you can easily see quote interpolation) 仅使用单个变量(您可以在其中轻松看到报价插值)
  • not escaping on your output, unnecessarily 不要不必要地逃避您的输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM