我想使用从返回 Json 结果的控制器操作传回的消息来显示 javascript 警报。我希望这条消息中有换行符。
这是我的控制器,注意输入换行符的“\n”:
[HttpPost]
public JsonResult CreatePO(PONewViewModel viewModel)
{
if (ModelState.IsValid)
{
var createPOResult = _managePOsAppServ
.CreateNewPOHeaderAndDetail(CurrentFacilityId, CurrentUserId, viewModel.VendorId,
viewModel.CustomerId, viewModel.OrderHeaderId, viewModel.ItemId, viewModel.QtyToOrder,
viewModel.UnitCost);
return Json(createPOResult, JsonRequestBehavior.AllowGet);
}
var modelStateErrors = this.ModelState.Keys.SelectMany(key => this.ModelState[key].Errors);
string errorMessage = "";
if (modelStateErrors.Count() > 0)
{
foreach (var error in modelStateErrors)
{
errorMessage += error.ErrorMessage + "\n";
}
}
return Json(ActionConfirmation<int>.CreateFailureConfirmation(errorMessage, -1,false).Message, JsonRequestBehavior.AllowGet);
}
但是,\n 正在警告框中显示。所以看起来返回的 Json 正在转义 \n。如何让它被识别为换行符?
Javascript:
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
alert(xhr.responseText);
hideLoading();
}
}