【发布时间】:2017-01-15 14:58:55
【问题描述】:
我需要替换页面的整个 HTML 或其正文内容(而不是在现有内容中附加更多 html)。
接受的答案here 向我展示了如何返回数据,但将其添加到“body”并相当工作。这是我现在拥有的 jQuery:
<script>
$(document).ready(function () {
$("#btnGetData").click(function () {
document.body.style.cursor = 'wait';
$.ajax({
type: 'GET',
url: '@Url.RouteUrl(routeName : "QuadrantData", routeValues : new { httpRoute = true , unit = "ABUELOS", begdate = "2016-08-20", enddate = "2016-08-27" })',
contentType: 'text/plain',
cache: false,
xhrFields: {
withCredentials: false
},
success: function (returneddata) {
$("body").remove;
$("body").append($(returneddata));
},
error: function () {
console.log('hey, boo-boo!');
}
}); // ajax
document.body.style.cursor = 'pointer';
}); // button click
}); // ready
</script>
...所以你可以看到我正在尝试首先删除正文中的 html,然后将返回的数据附加到正文中。
这个 REST 方法返回我想要的 html:
[System.Web.Http.HttpGet]
[System.Web.Http.Route("{unit}/{begdate}/{enddate}", Name = "QuadrantData")]
public HttpResponseMessage GetQuadrantData(string unit, string begdate, string enddate)
{
_unit = unit;
_beginDate = begdate;
_endDate = enddate;
string beginningHtml = GetBeginningHTML();
string top10ItemsPurchasedHtml = GetTop10ItemsPurchasedHTML();
string pricingExceptionsHtml = GetPricingExceptionsHTML();
string forecastedSpendHtml = GetForecastedSpendHTML();
string deliveryPerformanceHtml = GetDeliveryPerformanceHTML();
string endingHtml = GetEndingHTML();
String HtmlToDisplay = string.Format("{0}{1}{2}{3}{4}{5}",
beginningHtml,
top10ItemsPurchasedHtml,
pricingExceptionsHtml,
forecastedSpendHtml,
deliveryPerformanceHtml,
endingHtml);
return new HttpResponseMessage()
{
Content = new StringContent(
HtmlToDisplay,
Encoding.UTF8,
"text/html"
)
};
}
...但它附加了返回的 html 而不是替换它 - 原始正文 html 是完整的,并且返回的 html 出现在页面底部的下方。
如何替换而不是附加此 html?我尝试了 replacewith 和 replaceall,但这些对我不起作用。
【问题讨论】:
-
我会在 body 上使用 the .html 函数而不是
.remove和.append -
$("body").remove;什么? -
做 remove() 而不是 remove
-
我期待一个后续问题,询问为什么 ajaxed in 页面中的 javascript 不起作用。
标签: javascript jquery asp.net-web-api replaceall replacewith