gpt4 book ai didi

javascript - 从 asp.net 中的代码隐藏文件获取 json 到 javascript

转载 作者:太空宇宙 更新时间:2023-11-03 15:33:17 25 4
gpt4 key购买 nike

我有一个将我的数据集转换为 json 的 webstatic 方法,我想在我的 javascript 文件中使用该 json,但我的 div 中什么也没有得到。我在这里做错了什么,因为我是 asp.net 和 json 的新手。我必须在这里执行的简单任务是将我的 json 从代码隐藏文件获取到 javascript。

  <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div id="Result">Click here for the time.</div>
<script type="text/javascript">
$(document).ready(function () {
$("#Result").click(function () {
$.ajax({
type: "POST",
url: "A2_JVV.aspx/ds2json",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#Result").text(msg.d);
}

});
});
});

</script>

在我的 A2_JVV.aspx.cs 页面中,我使用 Newton json 将我的数据集转换为 json

   [WebMethod]
public static string ds2json()
{
DataSet ds = new DataSet();
ds=(DataSet)HttpContext.Current.Session["dsgrr"];
return JsonConvert.SerializeObject(ds.Tables["jv"], Formatting.Indented);
}

Chrome 控制台错误

    POST http://localhost:49388/WebSite2/A2_JVV.aspx/ds2json 500 (Internal Server Error)
c.extend.ajax @ jquery-1.4.2.min.js:130(anonymous function)
@ A2_JVV.aspx:207c.event.handle
@ jquery-1.4.2.min.js:55c.event.add.j.handle.o
@ jquery-1.4.2.min.js:49

最佳答案

试试这个。您没有调用正确的方法。

$.ajax({
type: "POST",
url: "A2_JVV.aspx/ds2json",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
var result = $.JSON(msg);
$("#Result").text(result.d);
}

});

关于javascript - 从 asp.net 中的代码隐藏文件获取 json 到 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32941433/

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