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

时间:2022-08-25 22:05:27

I have a webstatic method which converts my dataset into json and i want that json in my javascript file but i am getting nothing in my div here . What i am doing wrong here as i am new to asp.net and json . The simple task which i have to perform here is to get my json from code behind file to javascript .

我有一个webstatic方法将我的数据集转换为json,我想在我的javascript文件中使用json,但我在这里没有得到任何东西。我在这里做错了,因为我是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>

in my A2_JVV.aspx.cs page I have use Newton json to convert my dataset in json

在我的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 Console error

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

2 个解决方案

#1


0  

Try this. You are not calling the right method.

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

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

                 });

#2


0  

Try this, you need to add "/" at first of url :

试试这个,你需要在网址的第一个添加“/”:

url: "/A2_JVV.aspx/ds2json"

#1


0  

Try this. You are not calling the right method.

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

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

                 });

#2


0  

Try this, you need to add "/" at first of url :

试试这个,你需要在网址的第一个添加“/”:

url: "/A2_JVV.aspx/ds2json"