简体   繁体   English

从C#返回ajax错误

[英]Returning a ajax Error from C#

When one WebMethod it's called from ajax, if I return a simple string, the ajax code go to success method without errors, something like this: 当从ajax调用一个WebMethod时,如果我返回一个简单的字符串,则ajax代码将毫无错误地转到成功方法,如下所示:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string Hello()
{
    return "Hello World";
}

This is right, but, how is the correct way to make the WebMethod for return a error and this will be captured in ajax Error method instead of success method? 没错,但是,使WebMethod返回错误的正确方法是什么,而这将在ajax Error方法而不是成功方法中捕获?

Thanks 谢谢

Change your Response status Code : 更改您的响应状态代码:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string Hello()
{
    Response.StatusCode = 400 ; // Will be captured on ajax error method 
    return "Hello World";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM