【发布时间】:2012-01-24 18:34:56
【问题描述】:
我在 JQuery 中调用 ASP.NET Web 服务时遇到问题。我基本上遵循以下所有说明:
http://www.codeproject.com/Articles/211489/Using-JSON-with-ASP-NET-3-5
我已经用 C# 和 ASP.NET 编写了 Web 服务和控制台服务器。
我的 JQuery 如下:
jQuery.support.cors = true;
$.ajax({
type: "POST",
contentType: "application/json; charset=ut-8",
username: "John",
password: "Doe",
url: "https://machinename:8043/WtfService/HelloWorldPostSimple1",
data: '{"firstName":"John"}',
dataType: "json",
success: function(msg) {
alert(msg.d);
},
error: function(jqXHR, textStatus, errorThrown) {
alert(jqXHR + " : " + textStatus + " : " + errorThrown);
}
});
第一行允许在 IE 中进行跨域调用,这是可行的。如果我调用不接受参数的 Web 服务方法,它对 GET 和 POST 工作 100%。
我的web服务方法如下:
[WebInvoke(Method = "POST", UriTemplate = "HelloWorldPostSimple1")]
[PrincipalPermission(SecurityAction.Demand, Role = "WtfUser")]
public string HelloWorldPostSimple1(string firstName)
{
return string.Format("Hello {0} {1}", firstName, "Doe");
}
我已经尝试将请求和响应格式专门放入 JSON,但仍然没有运气。
【问题讨论】:
-
你试过在ajax中添加:
contentType: "application/json",吗? -
是的,它在 ajax 调用中
-
与您的问题没有直接关系,但我注意到您链接到的文章犯了手动 JSON 序列化响应的错误。确保你不要那样做。更多信息:encosia.com/…
-
我有一个类似的服务正在运行,但它在我的实例中的方法上有一个
[OperationContract]装饰器。 -
我不确定这是否会导致您的任何问题,但 contentType 字符集应该是 utf-8(您缺少“f”)
标签: jquery asp.net ajax wcf json