บริการ WCF + JSONP

ฉันมีบริการ wcf บน http://testwcfgrid.atic-solutions.com/ ซึ่งทุกอย่างทำงานได้ดี . ฉันต้องการใช้บริการนั้นในแอปพลิเคชันอื่นด้วย 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 และฉันก็ทำตามตัวอย่างของพวกเขา

ขอบคุณ


person Tomislav Trošić    schedule 30.06.2014    source แหล่งที่มา
comment
testwcfgrid.atic-solutions.com/Products.svc/Read ใช้ได้เมื่อ คุณเปิดดูมันเหรอ?   -  person Davie Brown    schedule 30.06.2014


คำตอบ (1)


เกี่ยวกับความคิดเห็นก่อนหน้าของฉัน - ดูเหมือนว่า url จะไม่ถูกต้องในการเรียกเมธอด Read

ฉันคิดว่าเป็นเพราะคุณไม่ได้ระบุคำจำกัดความ UriTemplate สำหรับการเรียกเมธอดแต่ละรายการ โปรดทราบว่าไม่ได้ระบุประเภทวิธีการ

อัปเดตวิธีการของคุณดังนี้:

[OperationContract]
[WebInvoke(Method = "GET",
           ResponseFormat = WebMessageFormat.Json,
           RequestFormat = WebMessageFormat.Json,
           UriTemplate = "Read")]
public DataSourceResult Read(int skip, int take, IEnumerable<Sort> sort, Filter filter)
person Davie Brown    schedule 30.06.2014
comment
ไม่ทำงาน ฉันสามารถใช้ GET ได้หรือไม่หากฉันมีพารามิเตอร์ นี่คือตัวอย่างที่ฉันติดตาม แต่ใช้งานได้เฉพาะในโดเมนเดียวกัน github.com/telerik/kendo-examples-asp-net/tree/master/ มีตัวอย่างการทำงานจริงของ wcf และ jsonp หรือไม่ - person Tomislav Trošić; 01.07.2014
comment
ฉันพบตัวอย่างนี้และใช้งานได้ codeproject .com/Articles/259832/ ทดสอบกับตารางเคนโด้ - person Tomislav Trošić; 01.07.2014