简体   繁体   English

使用JavascriptCallbackBehavior时如何抛出带有HTTP错误代码的WebFaultException?

[英]How to throw WebFaultException with HTTP error codes when using JavascriptCallbackBehavior?

I have a .Net server app (holding WCF REST JSONP service) and a javascript application on client side. 我在客户端上有一个.Net服务器应用程序(持有WCF REST JSONP服务)和一个javascript应用程序。

To avoid cross-domain calls from javascript side we use JSONP for communication. 为了避免从JavaScript端进行跨域调用,我们使用JSONP进行通信。 To make our life easier with the javascript callbacks from server side we are using JavascriptCallbackBehavior : WCF is wrapping all the JSON responses in the callback function. 为了简化服务器端的JavaScript回调,我们使用JavascriptCallbackBehavior :WCF将所有JSON响应包装在回调函数中。

[JavascriptCallbackBehavior(UrlParameterName = "$callback")]
[ServiceBehavior(...)]
public class MySvcREST : ISvcREST
{
   public bool SvcMethod()
   {
      throw new WebFaultException<string>("ERROR!", HttpStatusCode.InternalServerError);
   }
}

IF I use the code above I will get the following response from my service: 如果使用上面的代码,我将从服务中获得以下响应:

HTTP/1.1 200 OK
Content-Length: 31
Content-Type: application/x-javascript
Server: Microsoft-HTTPAPI/1.0
Date: Fri, 03 Aug 2012 11:43:26 GMT

angular.callbacks._1("ERROR!",500);

EDIT1: Actually even if I remove the JavascriptCallbackBehavior and leave the default callback name, I would have similar effect, which is not very good. EDIT1:实际上,即使删除JavascriptCallbackBehavior并保留默认的callback名称,我也会产生类似的效果,效果不是很好。

I really want to get the HTTP 500 error code, not a HTTP 200 OK with the error inside. 我真的想获取HTTP 500错误代码,而不是其中包含错误的HTTP 200 OK

How to solve my problem or is there another way to get JSONP from a WCF service? 如何解决我的问题,或者还有另一种从WCF服务获取JSONP的方法?

Unfortunately I didn't get any reply to my question, but I found the following, somewhat useful, link: http://msdn.microsoft.com/en-us/library/ee816916.aspx . 不幸的是,我没有得到任何答复,但是我发现以下一些有用的链接: http : //msdn.microsoft.com/en-us/library/ee816916.aspx

I understood that this behavior is by design ... although from javascript side (jquery / angular js), at this time of writing, there is no way to read the second returned parameter (in my case, the http error code). 我了解此行为是设计使然...尽管从javascript方面(jquery / angular js)来看,在撰写本文时,尚无法读取第二个返回的参数(在我的情况下为http错误代码)。

So, in the end I am not sure if there is a solution other than a workaround. 因此,最后我不确定是否有解决方法以外的解决方案。 For now we return a CustomResult in which we include a custom error code and message. 现在,我们返回一个CustomResult ,其中包含一个自定义错误代码和消息。

It is so disappointing with these web technologies when they don't blend with each other... you are forced to so many workarounds. 当这些网络技术无法相互融合时,它们会令人非常失望……您不得不采取许多解决方法。

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

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