WCF服务+ JSONP

时间:2014-06-30 10:31:31

标签: wcf jsonp

我在http://testwcfgrid.atic-solutions.com/上有wcf服务,一切正常。 我想在jQuery.ajax的另一个应用程序中使用该服务,但没有成功。 我的web.config是:

 <system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
  <remove name="WebDAVModule"/>
</modules>
<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="origin, content-type, accept" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
  </customHeaders>
</httpProtocol>
<handlers>
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<system.serviceModel>
<behaviors>
  <endpointBehaviors>
    <behavior name="ProductsAspNetAjaxBehavior">
      <enableWebScript />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="ProductsAspNetAjaxBehavior">
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceMetadata httpGetEnabled="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<standardEndpoints>
  <webScriptEndpoint>
    <standardEndpoint name="" crossDomainScriptAccessEnabled="true"/>
  </webScriptEndpoint>
</standardEndpoints>
<services>
  <service name="Products" behaviorConfiguration="ProductsAspNetAjaxBehavior">
    <endpoint address="" behaviorConfiguration="ProductsAspNetAjaxBehavior" binding="webHttpBinding" contract="Products" />
  </service>
</services>
</system.serviceModel>

我的wcf服务用于读取和创建:

[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json,
              RequestFormat = WebMessageFormat.Json)]
public DataSourceResult Read(int skip, int take, IEnumerable<Sort> sort, Filter filter)
{
    VoziloMassive table = new VoziloMassive();

    return table.GetVehicles().AsQueryable().Select(x => new VoziloViewModel
    {
        Id = x.Id,
        Timestamp = x.Timestamp,
        Marka = x.Marka,
        Registracija = x.Registracija,
        Vrsta = x.Vrsta,
        Flag = x.Flag
    }).ToDataSourceResult(take, skip, sort, filter);
}

[OperationContract]
public IEnumerable<VoziloViewModel> Create(IEnumerable<VoziloViewModel> vehicles)
{
    var result = new List<VoziloViewModel>();

    foreach (var vehicleViewModel in vehicles)
    {
        // Create a new Product entity and set its properties from productViewModel
        var vehicle = new VoziloViewModel
        {
            Marka = vehicleViewModel.Marka,
            Registracija = vehicleViewModel.Registracija
        };

        // store the product in the result
        result.Add(vehicle);

        // Add the entity
        VoziloMassive table = new VoziloMassive();
        table.AddVehicle(vehicle);
    }

    // Return the inserted products - the Kendo Grid needs their ProductID which is generated by SQL server during insertion

    return result.Select(x => new VoziloViewModel
    {
        Id = x.Id,
        Timestamp = x.Timestamp,
        Marka = x.Marka,
        Registracija = x.Registracija,
        Vrsta = x.Vrsta,
        Flag = x.Flag
    })
        .ToList();
}

我的.ajax电话:

$.ajax({
                        url: "http://testwcfgrid.atic-solutions.com/Products.svc/Read",               
                        dataType: "jsonp",
                        success: function (result) {
                            // notify the data source that the request succeeded
                            options.success(result);
                        },
                        error: function (result) {
                            // notify the data source that the request failed
                            options.error(result);
                        }
                    });

这是回应: http://prntscr.com/3xxdjw

我在这里缺少什么?我尝试了一切......我正在使用telerik kendo控件,我正在关注他们的例子。

由于

1 个答案:

答案 0 :(得分:0)

关于我以前的评论 - 看起来不像url对调用Read方法有效。

我认为这是因为您没有为每个方法调用指定UriTemplate定义。 另请注意,未指定Method类型。

按如下方式更新您的方法:

[OperationContract]
[WebInvoke(Method = "GET",
           ResponseFormat = WebMessageFormat.Json,
           RequestFormat = WebMessageFormat.Json,
           UriTemplate = "Read")]
public DataSourceResult Read(int skip, int take, IEnumerable<Sort> sort, Filter filter)