我想实现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;
}
}}
<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;
}
}}
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 });
});
});
你不放在毁掉函数里面就要将ajax设置成同步了
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 });
});