Java 脚本未在我的应用程序中执行
Posted
技术标签:
【中文标题】Java 脚本未在我的应用程序中执行【英文标题】:Java script isn't executing in my application 【发布时间】:2015-08-23 18:25:45 【问题描述】:以下是我的 html 代码:
<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Site.Master" CodeBehind="BranchRegistration.aspx.cs" Inherits="BloodBank.BranchRegistration" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div class="settings_pagecontent">
<div class="addform">
<asp:Button ID="BtnAdd" runat="server" CssClass="button" Text="Add" />
<br />
<br />
</div>
<div class="form">
<div class="settings_border-line">
<div class="settings_label_left">
<asp:Label ID="lblbranchname" class="settings_label_inv" runat="server" Text="Branch Name"></asp:Label>
<br />
<br />
<asp:Label ID="lblbranchphonenumber" class="settings_label_inv" runat="server" Text="Phone Number"></asp:Label>
<br />
<br />
</div>
<div class="text_right">
<asp:TextBox ID="txtbranchname" class="settings_textbox_body Branch_txt" runat="server"></asp:TextBox><br />
<br />
<asp:TextBox ID="txtbranchphonenumber" class="settings_textbox_body" runat="server"></asp:TextBox>
<asp:Label ID="lblbranchemailid" class="settings_label_inv" runat="server" Text="Email ID"></asp:Label>
<asp:TextBox ID="txtbranchemailid" class="settings_textbox_body" runat="server"></asp:TextBox>
</div>
<div class="clear"></div>
</div>
<br />
<br />
<div class="border-line">
<div class="label_left" style="width: 150px">
<asp:Label ID="lblbranchaddress" class="settings_label_inv" runat="server" Text="Branch Address"></asp:Label>
<br />
<br />
<asp:Label ID="lblbranchlandmark" class="settings_label_inv" runat="server" Text="Landmark"></asp:Label>
<br />
<br />
<asp:Label ID="lblTehsil" class="settings_label_inv" runat="server" Text="Tehsil"></asp:Label>
<br />
<br />
<asp:Label ID="lblbranchstate" class="settings_label_inv" runat="server" Text="State"></asp:Label>
<br />
<br />
<asp:Label ID="lbllocationmap" class="settings_label_inv" runat="server" Text="Location"></asp:Label>
<br />
<br />
</div>
<div class="text_right" style="width: 670px">
<asp:TextBox ID="txtbranchaddress" class="settings_textbox_body" runat="server"></asp:TextBox>
<asp:Label ID="lblbranchstreet" class="settings_label_inv" runat="server" Text="Street"></asp:Label>
<asp:TextBox ID="txtbranchstreet" class="settings_textbox_body" runat="server"></asp:TextBox>
<br />
<br />
<asp:TextBox ID="txtbranchlandmark" class="settings_textbox_body" runat="server"></asp:TextBox>
<asp:Label ID="lblbranchlocality" class="settings_label_inv" runat="server" Text="Locality"></asp:Label>
<asp:TextBox ID="txtbranchlocality" class="settings_textbox_body" runat="server"></asp:TextBox>
<br />
<br />
<asp:TextBox ID="txttehsil" runat="server" class="settings_textbox_body"></asp:TextBox>
<asp:Label ID="lblDistrict" class="settings_label_inv" runat="server" Text="District"></asp:Label>
<asp:TextBox ID="txtDistrict" runat="server" class="settings_textbox_body"></asp:TextBox>
<br />
<br />
<asp:DropDownList ID="ddlState" runat="server" class="settings_textbox_body"></asp:DropDownList>
<asp:Label ID="lblbranchpincode" class="settings_label_inv" runat="server" Text="Pincode"></asp:Label>
<asp:TextBox ID="txtbranchpincode" class="settings_textbox_body" runat="server"></asp:TextBox>
<br />
<br />
<asp:TextBox ID="txtbranchlocationmap" class="settings_locationmap" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="invSubmit" runat="server" Text="Submit" />
<asp:Button ID="invReset" runat="server" Text="Reset" />
</div>
<div class="clear"></div>
</div>
</div>
<div class="grid">
</div>
</div>
<script type="text/javascript">
$(document).ready(function ()
$('<%=txttehsil.ClientID%>').autocomplete(
source: function (request, response)
$.ajax(
type: "POST",
contentType: "application/json; charset=utf-8",
url: "BranchRegistration.aspx/GetNames",
data: "'name':'" + $('<%=txttehsil.ClientID%>').val() + "'",
dataType: "json",
success: function (data)
response(data.d);
,
error: function (err)
alert("Error: " + err);
);
, minLength: 1
);
);
</script>
<link href="Content/site1.css" rel="stylesheet" />
<link href="Content/Site.css" rel="stylesheet" />
</asp:Content>
服务器端代码如下:
[System.Web.Services.WebMethod]
public static List<string> GetNames(string name)
List<string> nameList = new List<string>();
string strSqlQuery = "Select * from SubRegions where SubRegionName like '%" + name + "%'";
SqlDataAdapter da = new SqlDataAdapter(strSqlQuery, Common.GetConnectionString());
DataSet ds = new DataSet(); da.Fill(ds, "SubRegions");
DataTable dt = ds.Tables["SubRegions"];
DataRowCollection drc = dt.Rows;
foreach (DataRow dr in drc)
nameList.Add(dr["SubRegionName"].ToString());
return nameList;
此代码在没有母版页的情况下运行,但未在我的应用程序中运行。请给我正确的答案。在此页面中使用autocomplete
文本框。它是jquery ajax application
。我在另一个项目中使用此代码,它正确执行并给了我正确的结果。
【问题讨论】:
console
中的任何错误?
你能告诉我们错误吗?您可以使用 firebug 来捕获错误。或者在 chrome 中使用 ctrl+shft+j
javascript 没有在这个应用程序中执行。自动完成文本框没有显示任何结果。它的工作就像简单的文本框。
删除您的代码并放置简单的警报以检查它是否正常工作。
你是否包含了jquery文件和自动完成js文件。
【参考方案1】:
在你的服务器代码中添加“System.Web.Script.Services.ScriptMethodAttribute()”,问题就解决了。
您的服务器代码如下所示
[System.Web.Services.WebMethod ,System.Web.Script.Services.ScriptMethodAttribute()]
public static List<string> GetNames(string name)
List<string> nameList = new List<string>();
string strSqlQuery = "Select * from SubRegions where SubRegionName like '%" + name + "%'";
SqlDataAdapter da = new SqlDataAdapter(strSqlQuery, Common.GetConnectionString());
DataSet ds = new DataSet(); da.Fill(ds, "SubRegions");
DataTable dt = ds.Tables["SubRegions"];
DataRowCollection drc = dt.Rows;
foreach (DataRow dr in drc)
nameList.Add(dr["SubRegionName"].ToString());
return nameList;
【讨论】:
以上是关于Java 脚本未在我的应用程序中执行的主要内容,如果未能解决你的问题,请参考以下文章
在我的应用程序中使用简单适配器未在 ListView 上显示图像
用户数据脚本未在我的自定义 AMI 上运行,但在标准 Amazon linux 中运行
IBM Worklight 6.0.0.1 - JavaScript 未在 jQuery Mobile 多页应用程序中执行