2011-09-06 160 views
3

我有一個使用jQuery調用WCF服務的ASP.NET頁面。該頁面將文本框控件的內容傳遞給該服務。該服務使用這個文本框的值來查找記錄,然後它返回一個POCO到jQuery來更新一堆其他控件的內容。jQuery調用WCF服務參數通過爲空

至少,這就是我努力使情況發生。

到目前爲止,我的問題是,傳遞到我的WCF操作字符串參數總是空。我想我不能正確處理jQuery部分。我可以找到很多jQuery調用WCF服務的例子,這些服務沒有參數,也沒有硬編碼參數,我花了很多時間在SO和其他站點上查看了很多問題。仍然沒有運氣。我甚至有另一個WCF服務,被同一頁面上的jQuery .autocomplete調用,這很好。

當我跟蹤網絡通信,我可以看到,我得到的請求經歷。該請求是這樣的:

GET /Services/UserByEmailService.svc/GetUserByEmail?{"email":%20"[email protected]"} 

這裏是我的jQuery代碼:

<script type="text/javascript"> 
    $("#txtEmail").blur(function (event) { 
     $.ajax({ 
      type: "GET", 
      contentType: "application/json; charset=utf-8", 
      url: "/Services/UserByEmailService.svc/GetUserByEmail", 
      data: '{"email": "' + $("#txtEmail").val() + '"}', 
      processData: false, 
      success: function (response) { 
       $("#lblUserID").html = response.d.UID; 
       $("#txtOrganization").html = response.d.Organization; 
       $("#txtPhone").html = response.d.Phone; 
       $("#txtName").html = response.d.Name; 
       $("#txtNotes").html = response.d.Name; 
       $("#hdnUserKey").html = response.d.Syskey; 
      } 
     }); 
    }); 
</script> 

這是我的WCF服務的代碼看起來像......

[ServiceContract(Namespace = "")] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class UserByEmailService 
{ 
    [OperationContract] 
    [ServiceKnownType(typeof(UserLookupResult))] 
    [WebGet(BodyStyle = WebMessageBodyStyle.WrappedRequest, 
     RequestFormat = WebMessageFormat.Json, 
     ResponseFormat = WebMessageFormat.Json)] 
    public UserLookupResult GetUserByEmail(string email) 
    { 
     UserLookupResult oResult = new UserLookupResult(); 
     try 
     { 
      using (DownloadDBEntities ctx = new DownloadDBEntities()) 
      { 
       DownloadDB.User oUser = ctx.Users.FirstOrDefault(a => a.Email == email); 
       if (oUser != null) 
       { 
        oResult.Syskey = oUser.Syskey.ToString(); 
        oResult.UID = oUser.UserID; 
        oResult.Name = oUser.Name; 
        oResult.Organization = oUser.Organization; 
        oResult.Phone = oUser.Phone; 
        oResult.Notes = oUser.Notes; 
       } 
      } 
     } 
     catch (Exception ex) 
     { // For debugging only... 
      System.Diagnostics.Debug.WriteLine(ex.ToString()); 
     } 
     return oResult; 
    } 
} 

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class UserLookupResult 
{ 
    public string Syskey { get; set; } 
    public string UID { get; set; } 
    public string Name { get; set; } 
    public string Organization { get; set; } 
    public string Phone { get; set; } 
    public string Notes { get; set; } 

    public UserLookupResult() 
    { 
     Syskey = string.Empty; 
     UID = string.Empty; 
     Name = string.Empty; 
     Organization = string.Empty; 
     Phone = string.Empty; 
     Notes = string.Empty; 
    } 
} 

我可以跟蹤驗證碼並在我期望的時候執行。但是,GetUserByEmail方法(email)的字符串參數始終爲空,儘管我可以看到通過請求傳入了一個值(請參見上文)。

任何人都可以指向我一個jQuery調用傳遞一個文本值的控制的模糊的WCF服務,以便可以使用控制內容進行查找的工作的例子嗎?有什麼可以看到我的JavaScript或WCF服務定義中出現錯誤?

編輯:這是我的第...

非常感謝IAbstractDownvoteFactor,馬特·菲利普斯和darthjit有關此問題的幫助。最後,我通過調用$.ajax呼叫的data參數的變體來解決問題。這是結束了,我的工作:

data: { "email": $("#txtEmail").val() }, 

注意什麼之間我本來並最終什麼工作是我的數據論證了作爲具有引用鍵值的JSON對象傳遞的差異。

我不知道這是要咬我最終如果此解決方案將推廣到具有多個參數的服務。由於我在Encosia's Blog中讀到的東西 - 我指出jQuery將嘗試對數據對象進行URL編碼,而不是直接將其傳遞到您的Web服務,因此我最初嘗試將數據參數編組爲JSON字符串。我甚至還看過另一篇博客文章,在這篇文章中我找不到,說你的外部引用是雙引號還是單引號是很重要的。

另外:馬特指出的那樣,我也需要有processData = true,我得到了默認情況下通過從$.ajax調用這個說法。

回答

1

如果您輸入字段ID的拼寫不準確txtEmail它將返回null,因爲它不會找到輸入和val()不會爲您做任何事情。你可以看看fiddler或者某種類型的http偵聽器來查看ajax調用發佈到的url本身是什麼嗎?

編輯: 看看選項processData$.ajax調用。 從jQuery文檔

processDataBoolean 
Default: true 
By default, data passed in to the data option as an object (technically, anything other  
than a string) will be processed and transformed into a query string, fitting to the  
default  
content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or  
other non-processed data, set this option to false 

嘗試改變,要真實,看看是否有幫助。

+0

IE 9網絡追蹤程序說http GET將轉到此URL(如最初所述):'GET /Services/UserByEmailService.svc/GetUserByEmail?{"email":%20"[email protected]「} 「我非常有把握地認識到控制正在被接受。 –

+0

好吧,那爲什麼它不是'/Services/UserByEmailService.svc/GetUserByEmail?email = jbrown @ mooseware.ca'我相信它試圖把你的json變成字符串電子郵件變量,並且它不理解它,所以它結束爲空。 –

+0

馬特。問題原來是jQuery如何解釋'data'參數以及'processData'的值。你可以在我編輯的問題中看到細節。非常感謝您的幫助。 –

0

一些建議:

  1. 您可能需要設置綁定的WebHttpBinding讓你的服務是REST-FUL
  2. 您可能需要設置URI模板這樣

[ OperationContract的] [WebGet(UriTemplate = 「GetUserBYEmail?電子郵件= {電子郵件}」)] 公共UserLookupResult GetUserByEmail(字符串email)

+0

darthjit,非常感謝您的建議。我嘗試了這些,並導致我編譯器錯誤和手動.config更改的兔子洞。 UriTemplate在'System.ServiceModel.Description.WebScriptEnablingBehavior'中播放不好。 –

1

這條線:

data: '{"email": "' + $("#txtEmail").val() + '"}', 

應該是:

data: {email: $("#txtEmail").val()}, 

jQuery的不能想當然地認爲你的字符串是一個對象,因此它只是發送字符串。但jQuery完全知道如何處理對象。

+0

IAbstract,謝謝你的回答。事實證明,我需要一個變化 - 看到我編輯的問題的細節。你讓我走上了解決之道。謝謝! –