Response.使用 JQuery 和 WCF 编写 JSONP
更新:
该服务无法激活,因为它不支持 ASP.NET 兼容性。此应用程序启用了 ASP.NET 兼容性。在 web.config 中关闭 ASP.NET 兼容模式,或将 AspNetCompatibilityRequirements 属性添加到服务类型,并将 requestsMode 设置为“允许”或“必需”。
当我尝试访问 wcf 服务时,出现此错误:原因是 HttpContext.Current 为 null,在这种情况下我该怎么办?有什么帮助吗?
未将对象引用设置为对象的实例。
System.Web.Script.Serialization.JavaScriptSerializer s = new System.Web.Script.Serialization.JavaScriptSerializer();
Person p = new Person() { FirstName = "First name", LastName= "last name" };
string json = s.Serialize(p);
System.Web.HttpContext.Current.Response.Write("jsoncallback" + json);} //error
UPDATE:
The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompatibilityRequirements attribute to the service type with RequirementsMode setting as 'Allowed' or 'Required'.
when i try to access wcf service i get this error: the reason is HttpContext.Current is null, what should i do in this case? any help?
Object reference not set to an instance of an object.
System.Web.Script.Serialization.JavaScriptSerializer s = new System.Web.Script.Serialization.JavaScriptSerializer();
Person p = new Person() { FirstName = "First name", LastName= "last name" };
string json = s.Serialize(p);
System.Web.HttpContext.Current.Response.Write("jsoncallback" + json);} //error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HttpContext
是一个 ASP.Net 构造。如果您希望能够在服务中访问它,则需要启用 ASP您的服务的.Net 兼容性。通过 web.config:
或以声明方式:
HttpContext
is an ASP.Net construct. If you want to be able to access it in your service, then you need to enable ASP.Net Compatibility for your service.Either through the web.config:
Or declaratively:
如果简单的响应写入适合您,那么请考虑使用简单的 HttpHandler(通过实现 IHttpHandler 接口)。 Response 对象并不意味着在 WCF 服务中使用...
如果 WCF 适合您(也许它提供的技术堆栈是您需要的),那么请考虑使用已有的 Plumming 来输出 json:
If simple response write is the thing for you, then consider using a simple HttpHandler (by implementing the IHttpHandler interface). The Response object is not meant be used in a WCF Service...
If WCF however is the thing for you (maybe the stack of tech it offers is something you need), then consider using the plumming already there to output json: