繁体   English   中英

从asp.net中文件背后的代码获取json到javascript

[英]Get json from code behind file in asp.net to javascript

我有一个webstatic方法,它将我的数据集转换为json,我希望在我的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);
                     }

                 });

尝试此操作,您需要在url的开头添加“ /”:

url: "/A2_JVV.aspx/ds2json"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM