簡體   English   中英

從jquery ajax調用wcf服務時發生未定義的錯誤

[英]undefined error at calling wcf service from jquery ajax

我正在嘗試從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);

您的代碼中有幾個錯誤:

  1. $('#customerName').valueOf()不返回文本框的值。 您應該為此使用$('#customerName').val()

    更好的是:“自動完成”窗口小部件在request參數中提供了值。 使用request.term而不是直接從元素中讀取它。

  2. 刪除data:"{}" 由於這是一個GET請求,因此jQuery會將數據添加到URL的末尾: /Service1.svc/Getuseremail/test?{}

  3. 根據配置和版本,WCF運行時將返回帶有或不帶有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 
                });
            });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM