2016-08-18 34 views
2

解決策なしで数時間にわたって次のエラーをデバッグしようとしました(キーボードを半分に分割したい)。助けてください。それが必要。ASP.NET JavaScript関数が定義されていません

<asp:Button ID="btnCalculate" runat="server" Text="Calculate" OnClientClick="callAjaxMethod()"/> 

JS:

function callAjaxMethod() 
{ 

    // e.preventDefault(); 

    $.ajax({ 
     type: "POST", 
     url: "SimpleAjax.asmx/IsLeapYear", 
     data: '{year: "' + $("#<%=txtYear.ClientID%>").val() + '" }', 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (response) { 
      if (response.d) { 
       $('#<%=txtResult.ClientID%>').val('Leap Year'); 
      } 
      else { 
       $('#<%=txtResult.ClientID%>').val('Not a Leap Year'); 
      } 
     }, 
     failure: function (response) { 
      $('#<%=txtResult.ClientID%>').val("Error in calling Ajax:" + response.d); 
     } 
    }); 
} 

コードASMXファイルから:

/// Summary description for SimpleAjax 
    /// </summary> 
    [WebService(Namespace = "http://automatedpumpcontroller.somee.com/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService] 
    public class SimpleAjax : System.Web.Services.WebService 
    { 

     [WebMethod] 
     [System.Web.Script.Services.ScriptMethod()] 

     public static bool IsLeapYear(int year) 
     { 
      return DateTime.IsLeapYear(year); 
     } 
    } 
} 

Unhandled exception at line 90, column 95 in http://localhost:51770/SimpleAjax.aspx

0x800a1391 - JavaScript runtime error: 'callAjaxMethod' is undefined

この方法は、ページ上のボタンから呼び出されています

+0

'script'ブロック内の任意の他のJavascriptコードはありますか?場合によっては、クライアントコードの別の部分に問題があるため、関数は未定義です。 – ConnorsFan

+0

VS 2015でそれをどのようにチェックするのですか? –

+0

'callAjaxMethod'が' script'ブロックの中にある場合、同じブロックの中に他のコードがありますか? – ConnorsFan

答えて

0

function callAjaxMethodが別のjsファイルにある場合は、そのファイルをページに次のように含める必要があります。

名前のJSファイルを使用すると、ヘッド

<script src="js\app.js"></script> 
+0

マスターページのヘッドにスクリプトを追加することで、これが機能しました。しかし今、このエラーが発生します。 1864行、http:// localhost:51770/Scripts/jquery-1.10.2.jsのカラム2の未処理の例外 0x800a139e - JavaScriptランタイムエラー:構文エラー、認識できない式:#<%= txtYear.ClientID%> –

+0

はい、あなたはaspのサーバータグを使用することはできませんので、jsファイルでは、aspxページ自体に使用することができます。そのためには、aspxページ自体に関数を含める必要があります。または、** $( "[id $ = 'txtYear']")のようなjqueryセレクタでserverタグを使用しているセレクタを置き換える必要があります。 –

0

にスクリプトタグを追加する必要がありSimpleAjax.aspx に、その後のjs \ app.jsに維持し、あなたのページがSimpleAjax.aspx

です私はあなたの問題がでていると思いますJSの参照。あなたはJSを含めましたか?

function callAjaxMethod() { 
 
    alert('in'); 
 
}

 
<input type="button" id="btn" onclick="callAjaxMethod();" />

+0

マスターの参照。今すぐこのエラーが表示されます。行1864の2列、http:// localhost:51770/Scripts/jquery-1.10.2.jsの未処理の例外 0x800a139e - JavaScriptランタイムエラー:構文エラー、認識できない式:#<%= txtYear .ClientID%> –

+0

$( "#<%= txtYear.ClientID%>")。ここにあなたが何を得ているのですか? –

+0

テキストボックス内の年 –

関連する問題