2016-08-18 4 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' 내에서 다른 자바 스크립트 코드가 있습니까? 경우에 따라 클라이언트 코드의 다른 부분에서 문제가 발생하여 함수가 정의되지 않은 경우가 있습니다. – ConnorsFan

+0

VS 2015에서 어떻게 확인합니까? –

+0

'callAjaxMethod'가'script' 블록 안에 있다면, 같은 블록 안에 다른 코드가 있습니까? – ConnorsFan

답변

0

function callAjaxMethod가 별도의 js 파일에있는 경우 페이지에 해당 파일을 포함해야합니다 (예 :

). 이름 JS 파일이 JS에 보관됩니다 \ app.js과 페이지는

SimpleAjax.aspx

입니다 당신은

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

마스터 페이지 헤드에 스크립트를 추가하여이 작업을 수행했습니다. 그러나 지금이 오류가 발생합니다. 1864 행의 2 행 : http : // localhost : 51770/Scripts/jquery-1.10.2.js 0x800a139e - JavaScript 런타임 오류 : 구문 오류, 인식 할 수없는 표현 : # <% = txtYear.ClientID %> –

+0

그래, 당신은 JSP 파일 <%= %>에 asp 서버 태그를 사용할 수 없기 때문에 그것은 aspx 페이지 자체에서 사용할 수 있어야합니다. 이를 위해 aspx 페이지 자체에 함수를 포함해야하거나 server 태그를 사용하는 선택기를 ** $ ("[id $ = 'txtYear']")와 같은 jquery 선택기로 바꿔야합니다. ** –

0

나는 당신의 문제를 함께 생각하는 머리에 스크립트 태그를 추가 할 필요가 SimpleAjax.aspx 에 JS 참조. JS를 포함 시켰습니까?

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

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

+0

추가됨 마스터의 참조 이제이 오류가 발생합니다. 1864 행의 처리되지 않은 예외 (http : // localhost : 51770/Scripts/jquery-1.10.2.js) 0x800a139e - JavaScript 런타임 오류 : 구문 오류, 인식 할 수없는 표현식 : # <% = txtYear .ClientID %> –

+0

$ ("# <% = txtYear.ClientID %>"). val() 여기서 무엇을 얻고 있습니까? –

+0

텍스트 상자에있는 연도 –

관련 문제