我想实现jquery动态从后台读取数据,其中有一个变量名称为source,是个数组,传入插件就可以显示数据了,但是不知道如何从后台(Handler.ashx)能生成像下面这样的数组变量,再赋值给source。我代码都贴上来了,请帮忙指正修改下! 谢谢各位大侠。//=================================== 这是Default.aspx =========================================<body>
  <div id='content'>
  <script type="text/javascript">
    
  $(document).ready(function () {
  var theme = $.data(document.body, 'theme');
    
  var source=[];
  if (theme == null || theme == undefined) theme = '';
  $.get("Handler.ashx",function(json){
  source=json;
  });
    
    
  //这是默认生成的数组,变量名称为source,我的想法是动态读取数据,就是 Handler.ashx
  //但是不知道如何从后台能生成像下面这样的数据变量,再赋值给source。
    
  // var source = [
  // 'Affogato',
  // 'Americano'
  //  ];
  // Create a jqxListBox
  $("#jqxWidget").jqxListBox({ source: source, width: '200', height: '250px', theme: theme });
    
  });
  </script>
  <div id='jqxWidget'>
  </div>
  </div>
</body>
//================================这是Handle.ashx==============================================<%@ WebHandler Language="C#" Class="Handler" %>using System;
using System.Web;public class Handler : IHttpHandler {
    
  public void ProcessRequest (HttpContext context) {
  context.Response.ContentType = "text/plain";
    
    
  context.Response.Write("Affogato,Americano");
    
    
  }
  
  public bool IsReusable {
  get {
  return false;
  }
  }}

解决方案 »

  1.   

    将listbox方法放到回调函数里面就行了  $(document).ready(function () {
      var theme = $.data(document.body, 'theme');
        
      var source=[];
      if (theme == null || theme == undefined) theme = '';
      $.get("Handler.ashx",function(json){
    $("#jqxWidget").jqxListBox({ source: json, width: '200', height: '250px', theme: theme });
        
      });
      });
        
      
      
      

  2.   

    放过,没用的, 刚找到的解决办法是,eval函数解析json数据, 但是我如果不想把listbox方法放到回调函数里面,怎么去定义source数组的全局变量呢?
      

  3.   

    你不是用jquery?定义一个属性可以直接将返回数据转成json
    你不放在毁掉函数里面就要将ajax设置成同步了
      

  4.   


    var source;$.ajax({
      type: "GET",
      url: "Handler.ashx",
      async: false,//设置同步
      dataType: "json",//定义返回格式
      success: function(json){
          source = json;
      }$("#jqxWidget").jqxListBox({ source: json, width: '200', height: '250px', theme: theme });
    });