【发布时间】:2015-02-11 05:42:42
【问题描述】:
我正在尝试从 jquery ajax 调用 WCF 服务,但我只收到未定义的错误。请帮我解决这个问题。我的服务运行良好,但我的问题是从 ajax 调用 WCF。我的代码在这里
$('#customerName').autocomplete({
source: function (request, response) {
var param ={email:$('#customerName').val()};
$.ajax({
url: "http://localhost:53925/Service1.svc/Getusermail/" + $('#customerName').valueOf(),
data:"{}",
dataType: "json",
type: "GET",
processData: true,
async:false,
contentType: "application/json; charset=utf-8",
error: function (XMLHttpRequest, textStatus, errorThrown)
{
var err = eval("(" + XMLHttpRequest.responseText + ")");
alert(err);
//console.log(err.Message);
},
success: function (data)
{
alert("correct code");
//response(data.d);
}
});
},
minLength: 1 //This is the Char length of inputTextBox
});
});
我也在 WCF 的 web.config 中添加了必需的配置。在此先感谢。我的服务代码在这里
public List<string> Getusermail(string email)
{
List<string> emailid = new List<string>();
string query = string.Format("SELECT email FROM nciuser WHERE email LIKE '%{0}%'", email);
//Note: you can configure Connection string in web.config also.
using (SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=mbci;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
emailid.Add(reader.GetString(0));
}
}
}
return emailid;
}
上述方法的接口是
[OperationContract(Name = "Getusermail")]
[WebGet(UriTemplate = "Getusermail/{email}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
List<string> Getusermail(string email);
【问题讨论】:
-
请突出显示您遇到错误的行..以获得更好的答案
-
这里它只调用错误函数代码而不是成功函数......即使我的服务运行良好
-
您的
Getusermail方法是如何定义的? -
错误函数定义错误。