使用ajax(jQuery)在Asp.net中获取数据的问题 [英] Problem in Fetching Data in Asp.net using ajax(jQuery)

查看:53
本文介绍了使用ajax(jQuery)在Asp.net中获取数据的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取数据但我无法这样做以下是代码Default.aspx

 <% @     Page    标题  = 主页   语言  =  C#    MasterPageFile   = 〜/ Site.Master    AutoEventWireup   =  true    CodeBehind   =  Default.aspx.cs   继承  =  project121._Default   %>  

< asp:Content runat = server ID = FeaturedContent ContentPlaceHolderID = FeaturedContent >

< / asp:Content >
< asp:内容 runat = 服务器 ID = BodyContent ContentPlaceHolderID = 主要内容 >

< script src = Scripts / jquery-1.7.1.js > < span class =code-keyword>< / script >
< script 类型 = text / javascript >
function GetStudents(){

$( #studentPanel)。html( < div style ='text-align:center; background-color:yellow; border:1px solid red;'> Please Wait .. ....获取数据< / div>
$ .ajax({
类型: POST
url: Default.aspx
data: {RollNo + data.d.SName + '}
dataType: json
contentType: application / json; charset = utf-8
成功:onSuccess,
错误:onError
});

}
function onSuccess(data){
var tableContent = < table border ='0'> +
< tr> +
< td> RollNo< / td> +
< td> SName< / td> +
< td> Fname< / td> +
< td>类< / td> +
< td> ;地址< / td> +
< / tr>;
for var i = 0 ; i< data.d.lenght; i ++){
tableContent + = < tr> ; +
< td> + data.d [ i] .RollNo + < / td> +
< td> + data.d [i] .SName + < / td> +
< td> + data.d [i] .Fname + < / td> +
< td> + date.d [i] .Class + < / td> +
< td> + date.d [i] .Address + < / td> +
< / tr>;
}

tableContent + = < / table> ;
$( #studentPanel)。html(tableContent);
}
function onError(data){


}


< / script >



< h3 > 这是数据库通过JSON获取< / h3 >
< 输入 id = btn 类型 = 按钮 value = 加载数据库 onclick = GetStudents() / >
< div id = studentPanel >
< / div >


< / asp:Content >





这里是我的Default.aspx.cs



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Script.Services;
使用 System.Web.Services;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;

命名空间 project121
{
public partial class _Default:Page
{
protected void Page_Load( object sender,EventArgs e)
{

}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
private static 列表< student> GetStudents()
{
List< student> allStudents = new 列表< student>();
使用(dbEntitiy dc = new dbEntitiy())
{

allStudents = dc.students.ToList();
}
return allStudents;


}
}
}



让我知道这段代码有什么问题

解决方案

#studentPanel)。html ( < div style ='text-align:center; background-color:yellow; border:1px solid red; '>请等待......获取数据< / div>


.ajax({
type: POST
url: Default.aspx
data: {RollNo + data.d.SName + '}
dataType:< span class =code-string> json
contentType: application / json; charset = utf-8
成功:onSuccess,
错误:onError
});

}
function onSuccess(data){
var tableContent = < table border ='0'> +
< tr> +
< td> RollNo< / td> +
< td> SName< / td> +
< td> Fname< / td> +
< td>类< / td> +
< td> ;地址< / td> +
< / tr>;
for var i = 0 ; i< data.d.lenght; i ++){
tableContent + = < tr> ; +
< td> + data.d [ i] .RollNo + < / td> +
< td> + data.d [i] .SName + < / td> +
< td> + data.d [i] .Fname + < / td> +
< td> + date.d [i] .Class + < / td> +
< td> + date.d [i] .Address + < / td> +
< / tr>;
}

tableContent + = < / table> ;


#studentPanel)。html(tableContent) );
}
function onError(data){


}


< / script >



< h3 > 这是数据库通过JSON获取< / h3 >
< 输入 id = btn 类型 = 按钮 value = 加载数据库 onclick = GetStudents() / >
< div id = studentPanel >
< / div >


< / asp:Content >





这里是我的Default.aspx.cs



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Script.Services;
使用 System.Web.Services;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;

命名空间 project121
{
public partial class _Default:Page
{
protected void Page_Load( object sender,EventArgs e)
{

}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
private static 列表< student> GetStudents()
{
List< student> allStudents = new 列表< student>();
使用(dbEntitiy dc = new dbEntitiy())
{

allStudents = dc.students.ToList();
}
return allStudents;


}
}
}



让我知道这段代码有什么问题


I want to fetch data but i am unable to do so Here is code Default.aspx

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="project121._Default" %>

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">

</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">

    <script src="Scripts/jquery-1.7.1.js"></script>
    <script  type="text/javascript">
        function GetStudents() {

            $("#studentPanel").html("<div style='text-align:center;background-color:yellow;border:1px solid red;'>Please Wait...... Fetching Data</div>")
            $.ajax({
                type: "POST",
                url: "Default.aspx",
                data: "{RollNo"+data.d.SName+"'}",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: onSuccess,
                error: onError
            });

        }
            function onSuccess(data) {
                var tableContent = "<table border='0'>" +
                    "<tr>" +
                                  "<td>RollNo</td>" +
                                  "<td>SName</td>" +
                                  "<td>Fname</td>" +
                                  "<td>Class</td>" +
                                  "<td>Address</td>" +
                    "</tr>";
                for (var i = 0; i < data.d.lenght;i++){
                    tableContent += "<tr>" +
                                  "<td>" + data.d[i].RollNo + "</td>" +
                                  "<td>" + data.d[i].SName + "</td>" +
                                  "<td>" + data.d[i].Fname + "</td>" +
                                  "<td>" + date.d[i].Class + "</td>" +
                                  "<td>" + date.d[i].Address + "</td>" +
                                    "</tr>";
                    }

                tableContent+="</table>";
                $("#studentPanel").html(tableContent);
            }
            function onError(data) {


            }


    </script>



     <h3> This is database Fetching through JSON</h3>
    <input id="btn" type="button" value="load database" onclick="GetStudents()" />
    <div id="studentPanel">
</div>


</asp:Content>



HERE IS my Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace project121
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
        private static List<student> GetStudents()
        {
            List<student> allStudents = new List<student>();
            using (dbEntitiy dc = new dbEntitiy())
            {

                allStudents = dc.students.ToList();
            }
            return allStudents;


        }
    }
}


Let Me know what is problem with this code

解决方案

("#studentPanel").html("<div style='text-align:center;background-color:yellow;border:1px solid red;'>Please Wait...... Fetching Data</div>")


.ajax({ type: "POST", url: "Default.aspx", data: "{RollNo"+data.d.SName+"'}", dataType: "json", contentType: "application/json; charset=utf-8", success: onSuccess, error: onError }); } function onSuccess(data) { var tableContent = "<table border='0'>" + "<tr>" + "<td>RollNo</td>" + "<td>SName</td>" + "<td>Fname</td>" + "<td>Class</td>" + "<td>Address</td>" + "</tr>"; for (var i = 0; i < data.d.lenght;i++){ tableContent += "<tr>" + "<td>" + data.d[i].RollNo + "</td>" + "<td>" + data.d[i].SName + "</td>" + "<td>" + data.d[i].Fname + "</td>" + "<td>" + date.d[i].Class + "</td>" + "<td>" + date.d[i].Address + "</td>" + "</tr>"; } tableContent+="</table>";


("#studentPanel").html(tableContent); } function onError(data) { } </script> <h3> This is database Fetching through JSON</h3> <input id="btn" type="button" value="load database" onclick="GetStudents()" /> <div id="studentPanel"> </div> </asp:Content>



HERE IS my Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace project121
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
        private static List<student> GetStudents()
        {
            List<student> allStudents = new List<student>();
            using (dbEntitiy dc = new dbEntitiy())
            {

                allStudents = dc.students.ToList();
            }
            return allStudents;


        }
    }
}


Let Me know what is problem with this code


这篇关于使用ajax(jQuery)在Asp.net中获取数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆