簡體   English   中英

將HTML返回XMLHttpRequest對象

[英]Returning Html To XMLHttpRequest Object

我正在對此代碼返回以字符串形式返回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"

該服務可正確創建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,而沒有所有額外的“ \\”?

我以前遇到過這樣奇怪的問題,通常總是歸結為中間件。

看起來像服務器端的變量插值問題。

您的中間件語言和/或框架是什么?

確保以干凈的方式打印到輸出流:

  • 僅使用單個變量(您可以在其中輕松看到報價插值)
  • 不要不必要地逃避您的輸出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM