2017-01-11 6 views
0

ユーザーが情報を入力したときに私のクエリを返そうとしています。しかし、テキストボックスにデータを入力すると、ランダムなHTMLコードのHTMLポップアップページが返されます。私はこれが問題かもしれないと思うので、なぜajaxコールバックが[webmethod]を見つけられないのかを理解することができます。私のweb.configファイル内asp.netとajaxでtypehead.jsを返すことができません

Default.aspxのコード

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <title>testing typescript</title> 
<link rel="stylesheet" href='http://cdnjs.cloudflare.com/ajax/libs/twitter- bootstrap/3.0.3/css/bootstrap.min.css' 
media="screen" /> 
<script type="text/javascript" src='http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js'></script> 
<script type="text/javascript" src='http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js'></script> 
<script type="text/javascript" src="http://cdn.rawgit.com/bassjobsen/Bootstrap-3-Typeahead/master/bootstrap3-typeahead.min.js"></script> 
<link rel="Stylesheet" href="https://twitter.github.io/typeahead.js/css/examples.css" /> 
<script type="text/javascript"> 
$(function() { 
    $('[id*=txtSearch]').typeahead({ 
     hint: true, 
     highlight: true, 
     minLength: 1 
     , source: function (request, response) { 
      $.ajax({ 
       url: '<%=ResolveUrl("/Default.aspx.cs/GetCustomers") %>', 
       data: "{ 'prefix': '" + request + "'}", 
       dataType: "json", 
       type: "POST", 
       contentType: "application/json; charset=utf-8", 
       success: function (data) { 
        items = []; 
        map = {}; 
        $.each(data.d, function (i, item) { 
         var id = item.split('-')[1]; 
         var name = item.split('-')[0]; 
         map[name] = { id: id, name: name }; 
         items.push(name); 
        }); 
        response(items); 
        $(".dropdown-menu").css("height", "auto"); 
       }, 
       error: function (response) { 
        alert(response.responseText); 
       }, 
       failure: function (response) { 
        alert(response.responseText); 
       } 
      }); 
     }, 
     updater: function (item) { 
      $('[id*=hfCustomerId]').val(map[item].id); 
      return item; 
     } 
    }); 
}); 
</script> 
</head> 
<body> 
Enter search term: 
<form runat="server"> 
<asp:TextBox ID="txtSearch" runat="server" CssClass="form-control" autocomplete="off" 
Width="300"/> 
<asp:HiddenField ID="hfCustomerId" runat="server" /> 
<asp:Button ID="Button1" Text="Submit" runat="server" OnClick="Submit" /> 
</form> 
    </body> 
</html> 

Default.aspx.cs(分離コードファイル)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.Services; 
using System.Configuration; 
using System.Data.SqlClient; 

public partial class _Default : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 

} 

protected void Submit(object sender, EventArgs e) 
{ 
    string customerName = Request.Form[txtSearch.UniqueID]; 
    string customerId = Request.Form[hfCustomerId.UniqueID]; 
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Name: " + customerName + "\\nID: " + customerId + "');", true); 
} 

[WebMethod] 

public static string[] GetCustomers(string prefix) 
{ 
    List<string> customers = new List<string>(); 
    using (SqlConnection conn = new SqlConnection()) 
    { 
     conn.ConnectionString = "<%$ ConnectionStrings:FormDataConnectionString %>"; 
     using (SqlCommand cmd = new SqlCommand()) 
     { 
      cmd.CommandText = "SELECT [ID], [name], FROM [Wireless_Order] where ContactName like @SearchText + '%'"; 
      cmd.Parameters.AddWithValue("@SearchText", prefix); 
      cmd.Connection = conn; 
      conn.Open(); 
      using (SqlDataReader sdr = cmd.ExecuteReader()) 
      { 
       while (sdr.Read()) 
       { 
        customers.Add(string.Format("{0}-{1}", sdr["name"], sdr["ID"])); 
       } 
      } 
      conn.Close(); 
     } 
    } 
    return customers.ToArray(); 
    } 
} 

私はこれが含まれている

<system.web> 
    <pages clientIDMode="Static"></pages> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5" /> 
    <webServices> 
    <protocols> 
     <add name="HttpGet"/> 
     <add name="HttpPost"/> 
    </protocols> 
    </webServices> 

</system.web> 

答えて

0

問題はここにあった。ファイルを見つけることが

Default.aspx/GetCustomers 

する必要が

/Default.aspx.cs/GetCustomers 

バックスペースをクリックしてテキストフィールドをクリアしてもエラーが表示されます。

関連する問題