2012-09-05 74 views
0

如何从ajax.I调用非静态void函数出错。 这是Ajax代码: -如何通过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

相关问题