如何解决WCF响应使用JSON而不是JSONP
| 好吧,我正在使用WCF服务来处理来自Web应用程序的请求并以JSONP格式进行响应。我尝试了所有可以找到的解决方案,研究了文档(http://msdn.microsoft.com/zh-cn/library/ee834511.aspx#Y200)和示例项目。 问题是响应对象(json)没有被URL中提供的回调包裹。 请求就像:http://localhost/socialApi/socialApi.svc/api/login?callback=callback&username=AAAAA&password=BBBB
Web.config看起来像:
<?xml version=\"1.0\"?>
<configuration>
<system.web>
<trace enabled=\"true\"/>
<compilation debug=\"true\" targetFramework=\"4.0\"><assemblies><add assembly=\"System.Data.Entity,Version=4.0.0.0,Culture=neutral,PublicKeyToken=*************\" /></assemblies></compilation>
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled=\"true\"/>
<services>
<service name=\"RestService.socialApi\">
<endpoint address=\"\" binding=\"webHttpBinding\" contract=\"RestService.IsocialApi\" bindingConfiguration=\"webHttpBindingJsonP\" behaviorConfiguration=\"webHttpBehavior\">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name=\"ServiceBehavIoUr\">
<!-- To avoid disclosing Metadata information,set the value below to false and remove the Metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled=\"true\" />
<!-- To receive exception details in faults for debugging purposes,set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults=\"true\" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name=\"webHttpBehavior\" >
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name=\"webHttpBindingJsonP\" crossDomainScriptAccessEnabled=\"true\"/>
</webHttpBinding>
</bindings>
<!--<serviceHostingEnvironment multipleSiteBindingsEnabled=\"true\" />-->
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests=\"true\" />
</system.webServer>
<connectionStrings>
<add name=\"AsrAppEntities\" connectionString=\"myconstring**********\" />
</connectionStrings>
</configuration>
而我的运营合同:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
using System.IO;
namespace socialApi
{
[ServiceContract]
public interface IsocialApi
{
[OperationContract]
[WebGet(
ResponseFormat = Webmessageformat.Json,BodyStyle = WebMessageBodyStyle.Bare,UriTemplate = \"/api/login?username={username}&password={password}\")]
JsonpAuthenticationResponse Login(string username,string password);
}
}
响应只是普通的json:
{\"Message\":\"unauthorized\",\"Status\":400,\"Token\":null}
而且我要:
callbackfunction({\"Message\":\"unauthorized\",\"Token\":null})
我认为它与Web.config有关,因为当我修改示例并调整Web.config时,看起来像我的示例不再起作用。你会说我查明了问题..但是没有。
为了提供尽可能多的信息,以下是示例中的可行解决方案:
Web.config:
<?xml version=\"1.0\"?>
<!-- copyright (c) Microsoft Corporation. All rights reserved. -->
<configuration>
<system.web>
<compilation debug=\"true\" targetFramework=\"4.0\" />
<authentication mode=\"None\" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests=\"true\"/>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled=\"true\"/>
<standardEndpoints>
<webScriptEndpoint>
<standardEndpoint name=\"\" crossDomainScriptAccessEnabled=\"true\"/>
</webScriptEndpoint>
</standardEndpoints>
</system.serviceModel>
</configuration>
和班级:
//----------------------------------------------------------------
// copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
namespace Microsoft.Samples.Jsonp
{
[DataContract]
public class Customer
{
[DataMember]
public string Name;
[DataMember]
public string Address;
}
[ServiceContract(Namespace=\"JsonpAjaxService\")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class CustomerService
{
[WebGet(ResponseFormat = Webmessageformat.Json)]
public Customer GetCustomer()
{
return new Customer() { Name=\"Bob\",Address=\"1 Example Way\"};
}
}
}
上面的示例返回一个jsonp对象。这是来自示例的调用:
function makeCall() {
var proxy = new JsonpAjaxService.CustomerService();
proxy.set_enableJsonp(true);
proxy.GetCustomer(onSuccess,onFail,null);
}
proxy.set_enableJsonp(true);也许是我在通话中缺少的东西?但是我无法在通话中添加此内容,因为我不是从同一解决方案中调用该服务。
那么,是什么导致正常的JSON响应而不是请求JSONP?
解决方法
问题出在出厂设置。在svc文件的编组中,我不得不将工厂更改为System.ServiceModel.Activation.WebScriptServiceHostFactory。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。