2017-02-17 214 views
0

我的設置是以下劍道UI電網的OData

  • IIS 8.5
  • KendoUI
  • Server 2012中
  • MSSQL

我非常新的OData的,只發現了約它由於kendo ui,它似乎是從sql獲取數據的最佳方式。

我成功地在C#上創建了一個asp.net web應用程序,以便能夠從我的服務器獲取odata,如果我在瀏覽器中使用它,它就像一個魅力,我得到我的信息json fromatted。

因爲即時通訊新的這裏我可能有錯誤在這裏,但我的REST風格的服務是在一個單獨的網站比我的主要網站,我將使用劍道網格。我有端口8080上的Restful,而我的端口80上的正常站點

這是我的代碼,我的網格正在生成。

$("#vehiclesGrid").kendoGrid({ 
 
         dataSource: { 
 
          type: "odata", 
 
          transport: { 
 
           read: { 
 
            url: "http://mydomain(security issues):8000/odata/GetVehiclesConfigureds", 
 
            dataType: "json" 
 
           }, 
 
           schema:{ 
 
            model:{ 
 
             fields:{ 
 
              displayName:{type: "string"}, 
 
              sensor:{type: "number"}, 
 
              alertFlag:{type: "number"} 
 
             } 
 
            } 
 
           } 
 
          } 
 

 
         }, 
 
         height: 550, 
 
         groupable: true, 
 
         sortable: true, 
 
         columns: [{ 
 
          field: "displayName", 
 
          title: "Display Name", 
 
          width: 100 
 
         }, { 
 
          field: "sensor", 
 
          title: "# of Sensors" 
 
         }, { 
 
          field: "alertFlag", 
 
          title: "Alert Pending" 
 
         }] 
 
        });
<div id="vehiclesGrid"></div>

和控制檯我得到以下錯誤的:

XMLHttpRequest cannot load http://mydomain(security issues):8000/odata/GetVehiclesConfigureds?%24inlinecount=allpages. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://mydomain(security issues)' is therefore not allowed access.

如果我去開發工具在網絡部分,我可以看到被正確檢索到的OData的信息,但網格是空的。所以我不知道它爲什麼不顯示它,或者即使它在控制檯上與該錯誤有關。

回答

2

看着這個錯誤,看起來你有一個CORS問題。所以在你的ODATA應用上啓用CORS。

通過代碼WebApiConfig.cs

config.EnableCors();

YourController.cs

[EnableCors(origins: "http://Yourdomain.website.com", headers: "*", methods: "*")] 
public class YourController: ODataController 
{ 
    //details 
} 
+0

我一定要安裝的東西這個工作?我的編譯器無法解析EnableCors() –

+0

另外,我的控制器中的起源應該是Web服務端口還是站點? –

+0

@ pato.llaguno起源是您的網站的端口 - 您的請求的來源/起源Odata控制器 – Jaya