通JSON数组从C#将jQuery [英] Pass JSON array from c# to jQuery
问题描述
我在做jQuery的自动完成。如果我把硬盘codded JSON阵列工作正常。但是,当我通过从C#数组失败。请帮帮忙,我花足够的时间与它和我被困!
这里是我的AutoComplete.aspx jQuery代码
<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){
变种的msgbox = $(#状态);
$阿贾克斯({
型:POST,
//页面名称(中,该方法应被调用)和方法名
网址:AutoControl.aspx /的GetData,
//否则如果不要想传递给服务器端功能的任何值的数据交给低于
数据空白行:{},
的contentType:应用/ JSON的;字符集= UTF-8 ,
数据类型:JSON,
成功:函数(MSG){
$(#状态)VAL(msg.d);
}
});
$('#<%= tags.ClientID%GT;')自动完成([C ++,Java的,PHP,ColdFusion的。 ] {
宽度:320,
最大:4,
亮点:假的,
多:真,
multipleSeparator:,
滚动:真实,
scrollHeight属性:300
});
});
< / SCRIPT>
这里是我的AutoComplete.aspx.cs C#代码
[System.Web.Services.WebMethod]
公共静态字符串的GetData()
{
返回\C ++ \ \java\,\php\;
}
我如何通过从C#到jQuery的JSON的数组。有了这个代码,我可以检索从C#,但由于某些原因JSON是不是读值的值
我想改变这样的代码:
$('# <%= tags.ClientID%>')自动完成([C ++,Java的,PHP,ColdFusion的]
到
$('#<%= tags.ClientID%>)。自动完成([ jsonArray_from_C#]
< DIV CLASS =h2_lin>解决方案
您是否尝试过返回一个字符串数组
的 http://encosia.com/2011/04/13/asp-net-web-services-mistake-手动JSON序列化/
不要试图去解析JSON,直接传递对象。
I'm doing jQuery autocomplete. Works fine if I put hard codded JSON array. But it fails when I pass the array from c#. Please help, I spend enough time with it and I'm stuck!
Here is my jQuery code in AutoComplete.aspx
<script type="text/javascript">
$(document).ready(function () {
var msgbox = $("#status");
$.ajax({
type: "POST",
//Page Name (in which the method should be called) and method name
url: "AutoControl.aspx/GetData",
//else If you don't want to pass any value to server side function leave the data to blank line below
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#status").val(msg.d);
}
});
$('#<%=tags.ClientID%>').autocomplete(["c++", "java", "php", "coldfusion"], {
width: 320,
max: 4,
highlight: false,
multiple: true,
multipleSeparator: " ",
scroll: true,
scrollHeight: 300
});
});
</script>
Here is my C# code in AutoComplete.aspx.cs
[System.Web.Services.WebMethod]
public static string GetData()
{
return "\"c++\", \"java\", \"php\"";
}
How do I pass the JSON array from C# to jQuery. With this code I could retrieve the values from c# but some reason JSON is not reading the values.
I want to change this code: $('#<%=tags.ClientID%>').autocomplete(["c++", "java", "php", "coldfusion"]
to
$('#<%=tags.ClientID%>').autocomplete([ jsonArray_from_C# ]
Have you tried returning an string array?
http://encosia.com/2011/04/13/asp-net-web-services-mistake-manual-json-serialization/
don't try to parse Json, pass the object directly.
这篇关于通JSON数组从C#将jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!