2013-03-12 56 views
3

我試圖從我的webapi服務器獲取數據。調試顯示ajax不會成功

GET http://localhost:62578/api/DB [HTTP/1.1 200 OK 2ms] 

但我不打成功,而是我打錯誤,沒有數據傳遞到錯誤的功能?

var records; 
var loadedData = new Array(); 

$.ajax({ 
    type: 'GET', 
    dataType: 'json', 
    accept: "application/json", 
    url: 'http://localhost:62578/api/DB', 
    //data: { a:'a' }, 
    // contentType: 'application/json; charset=utf-8', 
    success: function(data) { 
    //I never get here. 
     $.each(data, function (key, val) {//key is the row number, val is the object data below 
       $.each(val, function (key2, val2) {//key2 is the array element name, val2 is the data. 
        if (key == 0) loadedData.push(key2); //we only need one row of collumns 
       }); 
       records = key; 
       init(loadedData); 
       }); 
      }, 
      error: function(jqXHR, exception) { 
      //both return undefined. 
     } 
     }); 

webapi服務器日誌。

jttp = HTTP FYI(愚蠢的堆棧溢出)

iisexpress.exe Information: 0 : Request, Method=GET, Url=jttp://localhost:62578/api/DB, Message='jttp://localhost:62578/api/DB' 
iisexpress.exe Information: 0 : Message='DB', Operation=DefaultHttpControllerSelector.SelectController 
iisexpress.exe Information: 0 : Message='MvcApplication1.Controllers.DBController', Operation=DefaultHttpControllerActivator.Create 
iisexpress.exe Information: 0 : Message='MvcApplication1.Controllers.DBController', Operation=HttpControllerDescriptor.CreateController 
iisexpress.exe Information: 0 : Message='Selected action 'GetAllProducts()'', Operation=ApiControllerActionSelector.SelectAction 
iisexpress.exe Information: 0 : Operation=HttpActionBinding.ExecuteBindingAsync 
iisexpress.exe Information: 0 : Message='Action returned 'MvcApplication1.Models.DataBase[]'', Operation=ReflectedHttpActionDescriptor.ExecuteAsync 
iisexpress.exe Information: 0 : Message='Will use same 'JsonMediaTypeFormatter' formatter', Operation=JsonMediaTypeFormatter.GetPerRequestFormatterInstance 
iisexpress.exe Information: 0 : Message='Selected formatter='JsonMediaTypeFormatter', content-type='application/json; charset=utf-8'', Operation=DefaultContentNegotiator.Negotiate 
iisexpress.exe Information: 0 : Operation=ApiControllerActionInvoker.InvokeActionAsync, Status=200 (OK) 
iisexpress.exe Information: 0 : Operation=DBController.ExecuteAsync, Status=200 (OK) 
iisexpress.exe Information: 0 : Response, Status=200 (OK), Method=GET, Url=jttp://localhost:62578/api/DB, Message='Content-type='application/json; charset=utf-8', content-length=unknown' 
iisexpress.exe Information: 0 : Operation=JsonMediaTypeFormatter.WriteToStreamAsync 
iisexpress.exe Information: 0 : Operation=DBController.Dispose 
+0

你使用本地主機還是隻是一個'localhost:62578/api/DB'的例子?請確保添加'http://' – Aboodred1 2013-03-12 19:03:36

+0

jqXHR.status爲0, jqXHR.responseText未定義, 異常未定義 – user2144480 2013-03-12 19:05:12

+0

Stackoverflow不停地嘮叨我把http放在我的文章中,它不會讓我繼續使用http「\」 \在那裏。 – user2144480 2013-03-12 19:06:14

回答

0

我覺得你的JavaScript代碼位於不同的端口上;也許端口80,你的json請求位於端口62578. Ajax請求只允許在同一個域,協議和端口。

爲了解決您的問題,您需要使用JSONP數據類型或任何其他類似的機制(服務器端解決方案)。

結帳wikipedia瞭解更多關於JSONP的信息。

簽出此example關於如何使用JSONP與jQuery ajax調用。

結賬stackoverflow對於類似的問題。

+1

啊好的Aboodred1,我跟着你。 – user2144480 2013-03-15 13:52:37