WCF response in JSON instead of JSONP
Allright, I'm using a WCF service to handle requests from my web app and respond with a JSONP format. I tried all the solutions I could find, studied the documentation (http://msdn.microsoft.com/en-us/library/ee834511.aspx#Y200) and the example project.
The problem is the response object (json) does not get wrapped with the callback supplied in the URL.
Request is like:
http://localhost/socialApi/socialApi.svc/api/login?callback=callback&username=AAAAA&password=BBBB
Web.config looks like:
<?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>
And my operationcontract:
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);
}
}
The response is just normal json:
{"Message":"unauthorized","Status":400,"Token":null}
And I want:
callbackfunction({"Message":"unauthorized","Status":400,"Token":null})
I think it has something to do with the Web.config, because when I modify the example and adjust the Web.config so it looks like mine the example doesn't function anymore. You would say I pinpointed the problem.. but no.
To supply as much as information as possible, here is the working solution from the example:
Web.config:
<?xml version="1.0"?>
<!-- Copyright (c) Microsoft Corporat开发者_开发百科ion. 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>
And the class:
//----------------------------------------------------------------
// 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"};
}
}
}
The above example returns a jsonp object. This is the call from the example:
function makeCall() {
var proxy = new JsonpAjaxService.CustomerService();
proxy.set_enableJsonp(true);
proxy.GetCustomer(onSuccess, onFail, null);
}
proxy.set_enableJsonp(true); is maybe something I am missing in my call? But I can't add this in my call because I'm not calling the service from the same solution.
So any idea's about what's causing the normal JSON response instead of the request JSONP?
The problem was in the factory settings. In the marckup of the svc file I had to change the factory to System.ServiceModel.Activation.WebScriptServiceHostFactory.
精彩评论