我有一個使用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
調用這個說法。
IE 9網絡追蹤程序說http GET將轉到此URL(如最初所述):'GET /Services/UserByEmailService.svc/GetUserByEmail?{"email":%20"[email protected]「} 「我非常有把握地認識到控制正在被接受。 –
好吧,那爲什麼它不是'/Services/UserByEmailService.svc/GetUserByEmail?email = jbrown @ mooseware.ca'我相信它試圖把你的json變成字符串電子郵件變量,並且它不理解它,所以它結束爲空。 –
馬特。問題原來是jQuery如何解釋'data'參數以及'processData'的值。你可以在我編輯的問題中看到細節。非常感謝您的幫助。 –