2011-10-04 75 views
2

叫我有以下調用我的服務,它返回一個list<string>返回列表<string>從WCF服務的jQuery

當我運行此我得到了錯誤發生的消息。

$(document).ready(function() //executes this code when page loading is done 
{ 
    $.ajax({ 
     type: "POST", 
     url: "Services/pilltrakr.svc/getAllUsers", 
     data: "{}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (response) { 
      alert(response.d); 
     }, 
     error: function (message) { 
      alert("error has occured : " + message); 
     } 
    }); 
}); 

如何從我的WCF服務返回一個列表?

接口:

[OperationContract] 
[WebInvoke(Method = "POST", 
       BodyStyle = WebMessageBodyStyle.Wrapped, 
       ResponseFormat = WebMessageFormat.Json, 
       RequestFormat = WebMessageFormat.Json)] 
List<string> getAllUsers(); 

類:

public List<string> getAllUsers() 
{ 
    userAccount ua = new userAccount(); 
    List<string> myUserList = new List<string>(); 
    myUserList = ua.getAllUsers(); 

    return myUserList; 
} 

UPDATE:

我改變了BodyStyle屬性WrappedRequest,然後一切工作(我試過BOT h GET和POST)。數據然後返回。現在我的問題是爲什麼這個改變解決了這個問題?我是否總是需要包含BodyStyle?

[OperationContract] 
    [WebInvoke(Method = "POST", 
        BodyStyle = WebMessageBodyStyle.WrappedRequest, 
        ResponseFormat = WebMessageFormat.Json, 
        RequestFormat = WebMessageFormat.Json)] 
    List<string> getAllUsers(); 
+0

您能否包含錯誤信息? –

+0

我想知道你爲什麼使用POST檢索數據? –

+0

@ladislav - 它有所作爲嗎? – webdad3

回答

0

我將BodyStyle屬性更改爲WrappedRequest,然後一切正常(我嘗試使用GET和POST)。數據然後返回。現在我的問題是爲什麼這個改變解決了這個問題?我是否總是需要包含BodyStyle?