[英]undefined error at calling wcf service from jquery ajax
I'm trying to call WCF service from jquery ajax and i got undefined error only..Help me to solve this please.My Service works well but my problem is in calling WCF from ajax.My code is here 我正在尝试从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
});
});
I've added Required congiuration in web.config of WCF too..Thanks in Advance.And My service code is here 我也已在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;
}
And Interface for the above method is 上面方法的接口是
[OperationContract(Name = "Getusermail")]
[WebGet(UriTemplate = "Getusermail/{email}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
List<string> Getusermail(string email);
Several errors in your code: 您的代码中有几个错误:
$('#customerName').valueOf()
does not return the value of the text-box. $('#customerName').valueOf()
不返回文本框的值。 You should use $('#customerName').val()
for that. 您应该为此使用
$('#customerName').val()
。
Better yet: The Auto-complete widget supplies the value in the request
argument. 更好的是:“自动完成”窗口小部件在
request
参数中提供了值。 Use request.term
instead of reading it from the element directly. 使用
request.term
而不是直接从元素中读取它。
Remove data:"{}"
. 删除
data:"{}"
。 Since this is a GET
request, jQuery will add the data to the end of the URL: /Service1.svc/Getuseremail/test?{}
. 由于这是一个
GET
请求,因此jQuery会将数据添加到URL的末尾: /Service1.svc/Getuseremail/test?{}
。
Depending on the configuration and version, the WCF runtime will return an object either with or without the d
property. 根据配置和版本,WCF运行时将返回带有或不带有
d
属性的对象。 To play safe, you can use response(data.d || data)
. 为了安全起见,您可以使用
response(data.d || data)
。 This will pick the d
property if it exists, otherwise use the full object. 这将选择
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
});
I used this code @Markus and it is running now 我使用了这段代码@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
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.