简体   繁体   English

从服务器端asp.net引发ajax调用中的错误

[英]Raise error in ajax call from server side asp.net

I have an ajax call like this 我有这样的ajax电话

$http.post('../services/name.aspx', packageElement, { 'Content-type': 'application/json' }).
         success(function (data, status, headers, config) {    }).
         error(function(data, status, headers, config) { error function  });

And in server-side I am sending a json response for this request. 在服务器端,我正在为此请求发送一个json响应。 But now I got a condition when I won't be having any data to send to client, when this happens I need to raise the error function in my Javascript code. 但是现在我有了一个条件,当我没有任何数据要发送给客户端时,这种情况发生时,我需要在我的Javascript代码中引发错误函数。 Is this possible? 这可能吗? if not any other alternatives? 如果没有其他选择?

If you want to trigger error handler of ajax from the server side, you could throw an exception, which will trigger the error handler on the client. 如果要从服务器端触发ajax的错误处理程序,则可以引发异常,这将在客户端上触发错误处理程序。 Or set HttpResponse.StatusCode = HttpStatusCode.NoContent to indicate there's no data to return. 或设置HttpResponse.StatusCode = HttpStatusCode.NoContent表示没有数据要返回。

Response.StatusCode = (int)HttpStatusCode.NoContent;
Response.ContentType = "text/plain";
Response.Write("No data");

http://msdn.microsoft.com/en-us/library/system.net.httpstatuscode%28v=vs.110%29.aspx http://msdn.microsoft.com/zh-cn/library/system.net.httpstatuscode%28v=vs.110%29.aspx

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

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