本文介绍了如何从的webmethod返回错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假如我要打电话饰的WebMethod aspx页面的方法。所以我需要知道如何从我的webmethod返回错误结果jQuery的错误部分将被调用,并显示错误详细信息?

suppose i am going to call aspx page method decorated with webmethod. so i need to know that how to return error from my webmethod as a result jquery error portion will be invoked and show the error detail?

是一个示例code。通过对此我打电话给我的aspx方法。

below is a sample code by which i am calling my aspx method.

$.ajax({
    type: "POST",
    url: "./Default.aspx/GetData",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: AjaxSucceeded,
    error: AjaxFailed
});

[WebMethod]
public static string GetData()
{

}

因此​​,请在code方面帮助该如何从我的webmethod结果jQuery的错误部分返回错误将被调用,并显示错误详细...指导我的细节。谢谢

so please help in terms of code that how to return error from my webmethod as a result jquery error portion will be invoked and show the error detail...guide me in detail. thanks

推荐答案

我不知道是否有一个更的WebMethod 做的特异性的方式,但在ASP .NET你通常只将状态设置code为您的。事情是这样的:

I don't know if there's a more WebMethod-specific way of doing it, but in ASP.NET you'd generally just set the status code for your Response object. Something like this:

Response.Clear();
Response.StatusCode = 500; // or whatever code is appropriate
Response.End;

使用标准错误codeS是适当的方式通知了错误的消费HTTP客户端。结束之前,你也可以的Response.Write()你想要的任何消息发送响应。这些格式是标准化的要少得多,所以你可以创建自己的。但只要状态code准确地反映了响应,那么你的JavaScript或任何其他客户端消耗的服务将理解错误。

Using standard error codes is the appropriate way to notify a consuming HTTP client of an error. Before ending the response you can also Response.Write() any messages you want to send. The formats for those are much less standardized, so you can create your own. But as long as the status code accurately reflects the response then your JavaScript or any other client consuming that service will understand the error.

这篇关于如何从的webmethod返回错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 08:31