我有一个 ASP.NET 页面,它使用 jQuery 调用 WCF 服务。该页面将文本框控件的内容传递给服务。该服务使用此文本框值来查找记录,然后将 POCO 返回到 jQuery 以更新一堆其他控件的内容。
至少,这就是我正在努力实现的目标。
到目前为止,我的问题是传递到 WCF 操作中的字符串参数始终为 null。我想我一定没有正确处理 jQuery 部分。我可以找到很多 jQuery 调用 WCF 服务而不带参数或使用硬编码参数的示例,并且我花了很多时间在 SO 和其他网站上查看了很多问题。还是没有运气。我什至在同一页面上有另一个由 jQuery .autocomplete 调用的 WCF 服务,并且工作得很好。
当我跟踪网络流量时,我可以看到我正在收到一个请求。该请求如下所示:
GET /Services/UserByEmailService.svc/GetUserByEmail?{"email":%20"jbrown@mooseware.ca"}
这是我的 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
) 的字符串参数始终为 null,即使我可以看到通过请求传入了一个值(参见上文)。
谁能给我一个 jQuery 调用的工作示例,该调用将文本值传递到控件模糊的 WCF 服务,以便可以使用控件内容执行查找?您是否可以看到我的 JavaScript 或 WCF 服务定义中出现了错误?
编辑:这是我的解决方案...
非常感谢 IAbstractDownvoteFactor、Matt Phillips 和 darthjit 针对此问题提供的帮助。最后,我通过对 $.ajax
调用的 data
参数进行变体解决了该问题。这就是最终为我工作的结果:
data: { "email": $("#txtEmail").val() },
请注意,我最初的内容和最终的内容之间的区别在于,我的数据参数必须作为带有带引号的键值的 JSON 对象传递。
我不知道这最终是否会困扰我,或者这个解决方案是否会推广到具有多个参数的服务。由于我在 Encosia's Blog 中读到的内容,我最初尝试将数据参数编码为 JSON 字符串。 - 这表明 jQuery 将尝试对数据对象进行 URL 编码,而不是直接将其传递到您的 Web 服务。我什至看过另一篇博文,但目前找不到,其中说外引号是双引号还是单引号很重要。
另外:正如 Matt 指出的,我还需要 processData = true,
这是我通过从 $.ajax 中删除此参数而默认获得的
调用。
最佳答案
如果您的输入字段 id 拼写不完全 txtEmail
它将返回 null,因为它找不到输入并且 val()
不会执行任何操作为你。您可以查看 fiddler 或某种 http 监听器来查看 ajax 调用发布到的 url 本身是什么吗?
编辑:
查看 $.ajax
调用的 processData
选项。
来自 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
尝试将其更改为 true 并查看是否有帮助。
关于asp.net - jQuery 对 WCF 服务参数的调用为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7326858/