回复于JSONP使用jQuery和WCF [英] Response.Write JSONP using JQuery and WCF

查看:122
本文介绍了回复于JSONP使用jQuery和WCF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:

的服务不能被激活,因为它不支持ASP.NET兼容性。 ASP.NET兼容性为该应用程序启用。关闭ASP.NET兼容模式在web.config或AspNetCompatibilityRequirements属性添加到与RequirementsMode设置的服务类型为允许或必需。

当我尝试访问WCF服务我得到这个错误:原因是HttpContext.Current为null,我应该在这种情况下怎么办?任何帮助吗?

对象引用未设置到对象的实例。

  System.Web.Script.Serialization.JavaScriptSerializer S =新System.Web.Script.Serialization.JavaScriptSerializer();
        人p =新的Person(){名字=名字,姓氏=姓氏};
        串JSON = s.Serialize(对);
        System.Web.HttpContext.Current.Response.Write(jsoncallback+ JSON);} //错误


解决方案

的HttpContext 是一个ASP.Net结构。如果你希望能够访问它在你的服务,那么你就需要启用 ASP .NET兼容性的为您服务。

无论是通过在web.config:

 < system.serviceModel>
        < serviceHostingEnvironment aspNetCompatibilityEnabled =真/>
< /system.serviceModel>

或者声明:

<$p$p><$c$c>[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Required)]
公共类为MyService:IMyService {...}

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

解决方案

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:

<system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />  
</system.serviceModel>

Or declaratively:

[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Required)]  
public class MyService : IMyService { ... }

这篇关于回复于JSONP使用jQuery和WCF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆