我想提出一個AJAX調用返回HTML作爲本一串代碼服務..返回的HTML XMLHttpRequest對象
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>
這裏是C#代碼用於創建e html ..
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,因爲它沒有所有額外的「\」?
你可以使用jquery的'$ .ajax()'嗎? –
@TravisJ爲什麼大家總是默認使用jQuery。也許他試圖學習JavaScript在後臺做什麼或不能使用庫? – locrizak
@locrizak - 因爲它是標準化的並且使用純XMLHttpRequest可能會在某些瀏覽器中遇到問題。 –