从 jquery ajax 调用 wcf 服务时出现未定义错误
undefined error at calling wcf service from jquery ajax
我正在尝试从 jquery ajax 调用 WCF 服务,但我只收到未定义的错误..帮我解决这个问题 please.My 服务运行良好,但我的问题在于从 ajax.My 调用 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 中添加了 Required congiuration..谢谢 Advance.And 我的服务代码在这里
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);
您的代码中有几个错误:
$('#customerName').valueOf()
不是 return 文本框的值。你应该为此使用 $('#customerName').val()
。
更好的是:自动完成小部件在 request
参数中提供值。使用 request.term
而不是直接从元素读取它。
移除data:"{}"
。由于这是一个 GET
请求,jQuery 会将数据添加到 URL 的末尾:/Service1.svc/Getuseremail/test?{}
.
根据配置和版本,WCF 运行时将 return 带有或不带 d
属性 的对象。为了安全起见,您可以使用 response(data.d || data)
。这将选择 d
属性(如果存在),否则使用完整对象。
$('#customerName').autocomplete({
source: function (request, response) {
$.ajax({
url: "/Service1.svc/Getusermail/" + request.term,
dataType: "json",
type: "GET",
processData: true,
async: false,
contentType: "application/json; charset=utf-8",
error: function (xhr, textStatus, errorThrown) {
console.log(xhr.responseText);
},
success: function (data) {
response(data.d || data);
}
});
},
minLength: 1 //This is the Char length of inputTextBox
});
我使用了这段代码@Markus,现在是运行
$(function () {
$('#customerName').autocomplete({
source: function (request, response) {
var param =$("#customerName").val();
$.ajax({
url: "http://10.10.4.86:66/MBCI_Services/Service1.svc/Getusermail/" + $('#customerName').val(),
data:'',
dataType: "json",
type: "GET",
crossDomain:true,
processData: true,
async:false,
contentType: "application/json",
error: function (xhr, ajaxOptions, thrownError)
{
alert(thrownError);
// console.log(thrownError);
},
success: function (data)
{
response($.map(data, function (item) {
return {
value: item
}
}))
//alert("work aaitu");
}
//+ $('#customerName').val()
});
},
minLength: 1
});
});
我正在尝试从 jquery ajax 调用 WCF 服务,但我只收到未定义的错误..帮我解决这个问题 please.My 服务运行良好,但我的问题在于从 ajax.My 调用 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 中添加了 Required congiuration..谢谢 Advance.And 我的服务代码在这里
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);
您的代码中有几个错误:
$('#customerName').valueOf()
不是 return 文本框的值。你应该为此使用$('#customerName').val()
。更好的是:自动完成小部件在
request
参数中提供值。使用request.term
而不是直接从元素读取它。移除
data:"{}"
。由于这是一个GET
请求,jQuery 会将数据添加到 URL 的末尾:/Service1.svc/Getuseremail/test?{}
.根据配置和版本,WCF 运行时将 return 带有或不带
d
属性 的对象。为了安全起见,您可以使用response(data.d || data)
。这将选择d
属性(如果存在),否则使用完整对象。
$('#customerName').autocomplete({
source: function (request, response) {
$.ajax({
url: "/Service1.svc/Getusermail/" + request.term,
dataType: "json",
type: "GET",
processData: true,
async: false,
contentType: "application/json; charset=utf-8",
error: function (xhr, textStatus, errorThrown) {
console.log(xhr.responseText);
},
success: function (data) {
response(data.d || data);
}
});
},
minLength: 1 //This is the Char length of inputTextBox
});
我使用了这段代码@Markus,现在是运行
$(function () {
$('#customerName').autocomplete({
source: function (request, response) {
var param =$("#customerName").val();
$.ajax({
url: "http://10.10.4.86:66/MBCI_Services/Service1.svc/Getusermail/" + $('#customerName').val(),
data:'',
dataType: "json",
type: "GET",
crossDomain:true,
processData: true,
async:false,
contentType: "application/json",
error: function (xhr, ajaxOptions, thrownError)
{
alert(thrownError);
// console.log(thrownError);
},
success: function (data)
{
response($.map(data, function (item) {
return {
value: item
}
}))
//alert("work aaitu");
}
//+ $('#customerName').val()
});
},
minLength: 1
});
});