2010-03-25 33 views
1

代码背后:Asp.Net(C#)的Jquery Ajax和的WebMethod呼叫问题

[WebMethod] 
     public static string emp() 
     { 
      return "BlaBla"; 
     } 

ASPX页面:

$(document).ready(function() { 

      $.get("TestPage.aspx/emp", null, function(data) { 

       alert(data); 

     }) 
    }) 

消息框输出: TestPage.aspx上页码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml">  
<head><title> 
</title>  
    <style>  
     tr  
     { 
       background-color: red; 
       color: White;  
     } 
     </style> 

如何使返回字符串?

谢谢。

回答

3

使用

$(document).ready(function() { 
    // Add the page method call as an onclick handler for the div. 
    $("#Result").click(function() { 
    $.ajax({ 
     type: "POST", 
     url: ""TestPage.aspx/emp", 
     data: "{}", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function(msg) { 
     // Replace the div's content with the page method's return. 
     $("#Result").text(msg.d); 
     } 
    }); 
    }); 
}); 

在您的网页使用

<div id="Result">Click here to return the string</div>

+0

背后方法的代码也需要是静态的,并饰以'ScriptMethod'以及'WebMethod'。 – 2010-03-25 11:20:35

+0

http://geekswithblogs.net/frankw/archive/2008/03/13/asp.net-ajax-callbacks-to-web-methods-in-aspx-pages.aspx – 2010-03-25 11:22:40

+0

@Pandiya Chendur,我不能运行你的代码帮助 – Chicharito 2010-03-25 11:23:16