gpt4 book ai didi

c# - 将 JSON 数组从 c# 传递到 jQuery

转载 作者:行者123 更新时间:2023-11-30 15:08:14 25 4
gpt4 key购买 nike

我正在做 jQuery 自动完成。如果我放入硬编码的 JSON 数组,效果很好。但是当我从 c# 传递数组时它失败了。请帮忙,我花了足够的时间,但我被困住了!

这是我在 AutoComplete.aspx 中的 jQuery 代码

<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>

这是我在 AutoComplete.aspx.cs 中的 C# 代码

[System.Web.Services.WebMethod]
public static string GetData()
{
return "\"c++\", \"java\", \"php\"";
}

如何将 JSON 数组从 C# 传递到 jQuery。使用这段代码,我可以从 C# 中检索值,但由于某些原因,JSON 未读取这些值。

我想更改此代码: $('#<%=tags.ClientID%>').autocomplete(["c++", "java", "php", "coldfusion"]

$('#<%=tags.ClientID%>').autocomplete([ jsonArray_from_C# ]

最佳答案

你试过返回一个字符串数组吗?

http://encosia.com/2011/04/13/asp-net-web-services-mistake-manual-json-serialization/

不要尝试解析Json,直接传递对象

关于c# - 将 JSON 数组从 c# 传递到 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5805316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com