好吧,我正在使用WCF服務處理來自我的Web應用程序的請求並以JSONP格式響應。我嘗試了所有可找到的解決方案,研究了文檔(http://msdn.microsoft.com/en-us/library/ee834511.aspx#Y200)和示例項目。JSON代替JSONP的WCF響應
問題是響應對象(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>
我的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);
}
}
的響應只是正常的JSON:
{"Message":"unauthorized","Status":400,"Token":null}
我想:
callbackfunction({"Message":"unauthorized","Status":400,"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?