Ajax xMlHttpRequest.responseText没有显示我设置的内容(非常奇怪!)

时间:2015-10-09 03:50:06

标签: ajax xmlhttprequest asp.net-ajax

我有一个标准的Ajax调用,带有“Success”和“error”。错误部分与:

一样简单
error: function (xMlHttpRequest, textStatus, errorThrown) {
                 errorLabel.text("ERROR:  " + xMlHttpRequest.responseText); },

在我的C#后端,我看到有人建议使用这些代码:

Response.StatusCode = 409; //as long as not 200
Response.Clear();
Response.Write(msg);
Response.End();

我的errorLabel在我的LOCAL DEV MACHINE上验证后成功显示xMlHttpRequest.responseText。

然而,当我将所有内容发布到WINDOWS 2012 SERVER时,xMlHttpRequest.responseText中没有自定义的“msg”。相反,我在responseText中得到了“页面未显示,因为存在冲突”。 (我可以理解,状态代码409意味着存在冲突。)

那么在服务器上它会如何表现呢?我自己的错误消息似乎已被替换!

非常感谢!

*********************** C#代码******************

[HttpPost]
public ActionResult DoSomething()
{
    if (someconditiontrue)
    {
       return Json(new {value1=xxxx, value2=yyy})
    }
    else
    {
       Response.StatusCode = 409; //as long as not 200
       Response.Clear();
       Response.Write("my customized error message");
       Response.End();
    }

    return Content(string.Empty);
}

1 个答案:

答案 0 :(得分:0)

我通过不使用“error”作为返回结果来处理它。相反,我仍然使用Json {error =“我的自定义错误消息”)对象作为结果,并在“success:function(result){....}”块中显示相应的验证消息。