[英]calling a wcf 4.0 service from jquery($.ajax)
我在vs2010中开发了一个简单的wcf服务
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
// TODO: Add your service operations here
}
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
以下电话正在运作
protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.Service1Client p = new ServiceReference1.Service1Client();
Label1.Text= p.GetData(5);
}
但是当我试图从jquery调用它时它不起作用
$(".test").live("click", function () {
$.ajax({
type: "post",
url: "http://localhost/Service1.svc/GetData",
data: {value:'1'},
contentType: "application/json; charset=utf-8",
timeout: 10000,
processData: true,
dataType: "json",
success: function(d) {
alert(d);
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError.toString());
}
});
任何人都可以帮助我,因为它给了我不眠之夜。 提前致谢。
先生,试试吧
$(".test").live("click", function () {
$.ajax({
type: "post",
url: "http://localhost/Service1.svc/GetData",
data: "{'value':1}",
contentType: "application/json; charset=utf-8",
timeout: 10000,
processData: true,
dataType: "json",
success: function(d) {
alert(d);
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError.toString());
}
});
检查通过线路传递的JSON的数据类型(使用Fiddler2或您选择的浏览器的开发人员工具)。 我怀疑JSON将“1”作为字符串而不是整数传递。
看来问题出在您的数据参数中:
data: {value:'1'},
这不应该有单引号。 该服务正在尝试解析具有字符串类型的“value”变量的方法。 改为:
data: {value:1},
这应该有助于服务解决正确的服务方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.