【问题标题】:WCF response in JSON instead of JSONPJSON而不是JSONP中的WCF响应
【发布时间】:2011-08-05 10:12:45
【问题描述】:

好的,我正在使用 WCF 服务来处理来自我的 Web 应用程序的请求并以 JSONP 格式进行响应。我尝试了所有我能找到的解决方案,研究了文档 (http://msdn.microsoft.com/en-us/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","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 的原因有什么想法吗?

【问题讨论】:

    标签: asp.net wcf response jsonp


    【解决方案1】:

    问题出在出厂设置中。在 svc 文件的标记中,我不得不将工厂更改为 System.ServiceModel.Activation.WebScriptServiceHostFactory。

    【讨论】:

      猜你喜欢
      • 2012-02-15
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 2012-12-09
      • 2018-12-19
      • 1970-01-01
      相关资源
      最近更新 更多