2010-09-17 37 views
0

JavaScript没有从web方法获取值。使用json或其他便捷类返回对象值

它说未定义位于的CallMe()S值..

我的目标是从Web方法得到一个对象....使用JS中的数据..

什么我是否缺少?

using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Web.Script.Services; 

public partial class _Default : System.Web.UI.Page 
{ 
    Label lblGeneral; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     lblGeneral = textMessager; 
    } 
    [System.Web.Services.WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public static string ShowMessage() 
    { 

     return ExternalManager.Write(); 
    } 
} 

JS

// JScript File 
function CallMe() 
{  
    // call server side method 
    var s = PageMethods.ShowMessage(); 
    s = eval(s); 
} 



(function() { 
var status = true; 
var fetchService = function() { 

      if(status){ 
      CallMe(); 
      status = false; 
      } 


      setTimeout(fetchService, 5000); 
      status = true; 

     } 

window.onload = fetchService; 
}()) 

****使用util类******

using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Web.Script.Services; 
using System.Web.Script.Serialization; 
/// <summary> 
/// Summary description for ExternalManager 
/// </summary> 
public class ExternalManager 
{ 
    public ExternalManager() 
    { 
     // 
     // TODO: Add constructor logic here 
     // 
    } 
    public static string Write() 
    { 
     string s = "Okay Buddy" + DateTime.Today.ToLongDateString(); 
     JavaScriptSerializer jss = new JavaScriptSerializer(); 
     string serializedPerson = jss.Serialize(s); 
     return s; 
    } 
} 

农机技术: Asp.net 2.0 + Ajax的启用 C#

回答

1

这是一个AJAX调用。你不能写:

var s = PageMethods.ShowMessage(); 

,并期望立即使用s变量,因为调用是异步的(它会立即返回,但结果是只有更高版本后,服务器的响应)。你需要使用回调。这是一个完整的工作示例:

<%@ Page Language="C#" %> 
<%@ Import Namespace="System.Web.Services" %> 
<%@ Import Namespace="System.Web.Script.Services" %> 
<script type="text/C#" runat="server"> 
    [WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public static string ShowMessage() 
    { 
     return "Okay Buddy" + DateTime.Today.ToLongDateString(); 
    } 
</script> 
<!DOCTYPE html> 
<html> 
<head> 
    <title>Test</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <script type="text/javascript"> 
     function CallMe() { 
      // call server side method 
      PageMethods.ShowMessage(function (result) { 
       alert(result); 
      }); 
     } 

     (function() { 
      var status = true; 
      var fetchService = function() { 
       if (status) { 
        CallMe(); 
        status = false; 
       } 
       setTimeout(fetchService, 5000); 
       status = true; 
      } 

      window.onload = fetchService; 
     }()); 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"> 
     <asp:ScriptManager ID="scm" runat="server" EnablePageMethods="true" /> 
    </form> 
</body> 
</html> 

还要注意,你不应该手动使用JavaScriptSerializer。它由PageMethods在内部使用。在你的ShowMessage方法中,你也可以返回复杂的对象。

如果方法有例如两个参数,你会成功和错误回调之前将它们传递:

PageMethods.ShowMessage('param1', 'param2', onSucceed, onError);