从jquery ajax调用wcf服务时出现未定义的错误

时间:2015-02-11 05:42:42

标签: c# jquery ajax wcf

我正在尝试从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中添加了必需的congiuration ..谢谢你的进度。我的服务代码就在这里

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);

2 个答案:

答案 0 :(得分:2)

代码中有几处错误:

  1. $('#customerName').valueOf()不会返回文本框的值。您应该使用$('#customerName').val()

    更好的是:自动完成小部件提供request参数中的值。使用request.term而不是直接从元素中读取它。

  2. 删除data:"{}"。由于这是GET请求,jQuery会将数据添加到网址的末尾:/Service1.svc/Getuseremail/test?{}

  3. 根据配置和版本,WCF运行时将返回具有或不具有d属性的对象。为了安全起见,您可以使用response(data.d || data)。这将选择d属性(如果存在),否则使用完整对象。

  4. $('#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  
    });
    

答案 1 :(得分:2)

我使用了这段代码@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 
                });
            });