2012-09-05 4 views
0

ajax에서 정적이 아닌 void 함수를 호출하는 방법. 오류가 발생했습니다. 이 아약스 코드는 다음과 같습니다 -ajax에 의한 비 정적 함수 void 호출 방법은 무엇입니까?

$('#button2 button').click(function() { 

       $.ajax({ 
       type: "POST", 
       url: "practiced_final.aspx/display", 
       data: "{}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       async: true, 
       cache: false, 
       success: function() 
       { 
       }, 
       error: function (a, b, c) { 
        alert(a + b + c); 
       } 
      }) 
           return false; 

      }); 

이것은 C#을 방법 코드 :

 [WebMethod] 
protected void display() 
    { 

    HttpContext.Current.Response.Write("Hello"); 
    } 

이것은 오류 메시지 - 나는 무엇을 놓치고

[object XMLHttpRequest]errorundefined 

?

도와주세요.

감사합니다. 이 함수는 정적이어야 작동하려면

+0

추가 속성을 메소드'display' [Webmethod]에. 나는 당신의이 방법이 공개되어야하는지 확신하지 못합니다. – harry180

+0

나는 이미 그것을 추가했지만 여기에 붙이지 않았습니다. 여전히 작동하지 않았습니다. 업데이트 된 코드를 확인하십시오. – user1627138

+0

이 질문에 대한 답변을 찾을 수있는이 [http://stackoverflow.com/questions/1360253/call-non-static-method-in-server-sideaspx-cs-from-client-side-use-javascript] 관련 질문 –

답변

1

, 그것은 다음과 같아야합니다

[Webmethod] 
public static void display() 
{ 
    HttpContext.Current.Response.Write("Hello"); 
} 

을 당신이 당신의 기능은 당신이 그것을 변경해야하는 문자열 반환 할 경우 :

[Webmethod] 
    public static string display() 
    { 
     return "Hello"; 
    } 
+0

여전히 오류가 발생합니다 : - [object XMLHttpRequest] parsererrorundefined – user1627138

+0

JS 오류가 발생합니까? – kleinohad

+0

반환하는 것은 JSON이 아니기 때문일 수 있습니다. – Stilgar