为什么Webmethod在使用ajax时不会触发

时间:2014-11-20 22:21:31

标签: javascript jquery asp.net ajax vb.net

我有一个用户控件(带有两个TextBox和一个标签)在Prerender上的用户控件的代码后面我使用RegisterStartupScript来调用一个调用外部JS函数的JS函数,它调用WebMethod。一切正常,直到调用Webmethod。使用AJAX调用webmethod时。 Web方法不会启动。我不知道为什么。任何帮助将不胜感激。

SpecificExperienceControl.ascx

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="SpecificExperienceControl.ascx.vb" Inherits="TardyAbsenteeReport.SpecificExperienceControl" %>
<asp:HiddenField ID="hdnID" runat="server" />
 <div class="SpecExp">
    <asp:TextBox ID="TextBox1" runat="server" CssClass="SpecExp-txtEmpID" ></asp:TextBox>   
    <asp:Label ID="Label1" runat="server" Text="Label" CssClass="SpecExp-txtLbl"></asp:Label>
    <asp:TextBox ID="TextBox2" runat="server" CssClass="SpecExp-txtAcode"></asp:TextBox>
 </div>

SpecificExperienceControl.ascx.vb

Public Class SpecificExperienceControl
Inherits System.Web.UI.UserControl
Dim number As Int16 = 0
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub
Public Property Value As String
    Get
        Return hdnID.UniqueID
    End Get
    Set(value As String)
        hdnID.Value = value
    End Set
End Property

'This function sends the necessary javascript to the page
Protected Sub Me_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender

    Dim cs = Page.ClientScript
    Dim myType = Me.GetType()

    'gets one total copy of JQuery
    If Not cs.IsClientScriptBlockRegistered(myType, "jquery") Then
        cs.RegisterClientScriptBlock(myType, "jquery", GetJQueryInclude())
    End If

    'gets one total copy of the toggle function in SpecificExp.js
    If Not cs.IsClientScriptIncludeRegistered(myType, "helper") Then
        cs.RegisterClientScriptInclude(myType, "helper", VirtualPathUtility.ToAbsolute("~/SpecificExperienceControl.js"))
    End If

    'gets one copy for each user control on the page of the js in the getStartupScript function below
    cs.RegisterStartupScript(myType, "regEvents" & Value, getStartupScript(), True)

End Sub
Private Function getStartupScript() As String
    Return "$(function(){$('#" & TextBox1.ClientID & "').change(function(){toggle('" &
        TextBox1.ClientID & "','" & Label1.ClientID & "','" & TextBox2.ClientID & "')})});"
End Function

'This function get jquery include string
Public Function GetJQueryInclude() As String
    Const CurrentJQueryVersion = "2.1.1"
    Const formatString = "<!--[if {0}><script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/{1}/jquery.min.js'></script><!{2}[endif]-->"
    Return String.Format(formatString, "lte IE 8]", "1.11.1", String.Empty) &
        String.Format(formatString, "gt IE 8]", CurrentJQueryVersion, String.Empty) &
        String.Format(formatString, "!IE]>--", CurrentJQueryVersion, "--<!")
End Function

结束班

SpecificExperience.aspx

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="SpecificExperience.aspx.vb" Inherits="TardyAbsenteeReport.SpecificExperience" %>
<%@ Register Src="~/SpecificExperienceControl.ascx" TagName="SpecExp" TagPrefix="HR" %>

 <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
 </asp:Content>
 <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:Repeater ID="Repeater1" runat="server">
     <ItemTemplate>
       <hr:SpecExp runat="server"></hr:SpecExp> 
     </ItemTemplate>
   </asp:Repeater>
 </asp:Content>

SpecificExperience.aspx.vb(这里我只是将Repeater绑定到一个生成25个空行的XML文件)

Public Class SpecificExperience
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim xmldatasource2 As XmlDataSource = New XmlDataSource With
                                          {.DataFile = "/App_Data/EmptyRows.xml"}
    Repeater1.DataSource = xmldatasource2
    Repeater1.DataBind()
End Sub
End Class

SpecificExperienceControl.js

function toggle(txtId, lblname, txtcode) {
$.ajax({
    type: "POST",
    url: "SearchEmpId.asmx/GetEmployeeName",
    data: '{id: "it works"}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (data) {
        $('#' + lblname).val(data.d);
    }
 });    
}

SearchEmpId.asmx.vb一切正常,但这不会被激发

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports Newtonsoft.Json

<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class SearchEmpId
Inherits System.Web.Services.WebService
<WebMethod()>
Public Shared Function GetEmployeeName(ByVal id As String) As String
    Return "itworks"
    End Function
End Class

以下是我在收到请求时所获得的内容的屏幕截图

enter image description here

当我点击Response Body时,我看到了这一点。为什么找不到网络方法?

enter image description here

0 个答案:

没有答案