c#后台生成数组数据传递给jquery前端问题

dihaokelou 2011-11-19 12:22:55
我想实现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;
}
}

}
...全文
252 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
liangws 2011-11-19
  • 打赏
  • 举报
回复

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 });
});


liangws 2011-11-19
  • 打赏
  • 举报
回复
你不是用jquery?定义一个属性可以直接将返回数据转成json
你不放在毁掉函数里面就要将ajax设置成同步了
dihaokelou 2011-11-19
  • 打赏
  • 举报
回复
放过,没用的, 刚找到的解决办法是,eval函数解析json数据, 但是我如果不想把listbox方法放到回调函数里面,怎么去定义source数组的全局变量呢?
[Quote=引用 1 楼 liangws 的回复:]

将listbox方法放到回调函数里面就行了
JScript code

$(document).ready(function () {
var theme = $.data(document.body, 'theme');

var source=[];
if (theme == null || theme == undefined) theme = '';
$.……
[/Quote]
liangws 2011-11-19
  • 打赏
  • 举报
回复
将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 });

});
});



dihaokelou 2011-11-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 liangws 的回复:]

JScript code

var source;

$.ajax({
type: "GET",
url: "Handler.ashx",
async: false,//设置同步
dataType: "json",//定义返回格式
success: function(json){
source = json;
}

$("#jqxWidget").jqxLi……
[/Quote]
解决了,谢谢

87,989

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧