2015-02-11 41 views
2

我想從jquery ajax調用WCF服務,我只有undefined錯誤..幫我解決這個please.My服務工作正常,但我的問題是調用WCF從ajax.My代碼是在這裏從jquery ajax調用wcf服務undefined錯誤

$('#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 too..Thanks的web.config文件所需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); 
+0

請彰顯你在哪裏得到一個error..for更好的答案 – User125 2015-02-11 06:07:56

+0

此行就只調用錯誤函數代碼但不是成功函數......即使我的服務工作正常 – 2015-02-11 06:17:03

+0

如何定義「Getusermail」方法? – 2015-02-11 06:24:06

回答

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

幾個錯誤,在你的代碼:

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