2013-05-02 66 views
0

這對我來說有點神奇,但我從一個在線教程中創建了一個WCF服務,該教程顯示了一些SQL數據(運行ASP.NET解決方案本地生成服務的結果所以我認爲它運行正常)。如何使用Ajax循環遍歷WCF

我想要做的是從html頁面連接到這個服務,這是我創建的腳本。

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"> </script> 
<script type="text/javascript"> 
$(function() { 
    // Send an AJAX request 
    alert("running"); 

    $.ajax({ 
    type: "GET", 
    url: "http://localhost:15021/Service1.svc/getAllCustomers", 
    dataType: "json", 
    success: alert("Success"), 
    error: alert("Failure") 
    }); 

}); 


</script> 

我沒有得到任何錯誤,但我只是得到2個警報(成功和失敗),所以我的問題是如何將我真正開始與該WCF被返回的數據?

任何建議將是偉大的。

謝謝, 克雷格

回答

0
success: function(response){ 
    //Do the things you want to do with the response data you are getting. 
    //And yes you are getting json response, if not there will be errors 
} 
failure: function(x,e){ 
    //Do the things you want to do in case of failure. 
} 
0

你需要做一些事情,如果任何被所有返回時,返回的數據。試着增加某種回調函數的成功的:設置,如:

function (msg) { 
    if (msg !== null) { 
    alert(msg); 
} 

和其他功能的錯誤:設置,所以你可以看到你做了什麼錯誤:

function (msg) { 
    alert(msg.status + " " + msg.statusText); 
} 
+0

嗨,歡呼都。顯然它不能正常工作,因爲輸入:\t成功:function(response){alert(「success」)}, \t \t error:function(response){alert(「failure」)} 現在只顯示錯誤警報並查看Chrome中的資源選項卡,其中突出顯示 XMLHTTPRequest無法加載http:// localhost:15021/Service1.svc/getAllCustomers。來源null是不允許的訪問控制 - 允許來源 我正在運行Chrome的--allow文件訪問從文件標記爲EXE(我認爲這可能是)但唉!現在我真的很掙扎! – SxChoc 2013-05-02 15:36:53

+0

嘗試進行調試並進入Jquery,如果從WCF獲取的響應不是JSON格式,則會在JQuery實現內部處理無效的JSON eror。我爲我的ajax調用寫了相同的代碼,它正在工作。 – Guanxi 2013-05-02 17:11:19

+0

此外,您應該嘗試錯誤:函數(響應){警報(響應)}而不是錯誤:函數(響應){警報(「失敗」)},並調試您的響應中的數據,狀態和statusText屬性有錯誤信息通常。 – Popo 2013-05-02 19:35:28