这是 jquery ajax 调用 aspnet mvc 控制器的有效数据吗?

发布于 2024-08-31 00:55:07 字数 2159 浏览 6 评论 0原文

我正在将 jquery 与 asp.net mvc 结合使用...我的数据选项是否有效或者我是否缺少某些内容...

$.ajax({
            type:"POST",
            url: "Materials/GetRecords",
            data: "{'currentPage':1,'pageSize':5}",

任何建议...

编辑: 我正在从视图页面调用一个函数,

 <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript">
        $(document).ready(function() {
            getMaterials(0);
          });
    </script>

我的函数是,

function getMaterials(currentPage) {
    $.ajax({
        type:"POST",
        url: "Materials/GetRecords",
        data: "{'currentPage':" + (currentPage + 1) + ",'pageSize':5}",
        contentType: "application/json; charset=utf-8",
        cache: false,
        global: false,
        async: false,
        dataType: "json",
        success: function(data) {
            var divs = '';
            $("#ResultsDiv").empty();
            $.each(data.Results, function() {
                divs += '<div class="resultsdiv"><br /><span style="display: inline-block;width:150px;" class="resultName">' + this.Mat_Name + '</span><span class="resultfields" style="padding-left:10px;">Measurement&nbsp;:</span>&nbsp;<span class="resultfieldvalues">' + this.Mes_Name + '</span>&nbsp;<a href="/Materials/Delete/' + this.Id + '">Delete</a>&nbsp;<a href="/Materials/Details/' + this.Id + '">Details</a>&nbsp;<a href="/Materials/Edit/' + this.Id + '">Edit</a></div>';
            });
            alert("hai");
            $("#ResultsDiv").append(divs);
            $(".resultsdiv:even").addClass("resultseven");
            $(".resultsdiv").hover(function() {
                $(this).addClass("resultshover");
            }, function() {
                $(this).removeClass("resultshover");
            });
            $("#HfId").val("");
            $("#HfId").val(data.Count);
        }
    });
    return false;
}

在视图页面加载上我看到一条警告,内容为 [Object object].. 为什么这个...

I am using jquery with asp.net mvc.... Is my data option valid or am i missing some thing...

$.ajax({
            type:"POST",
            url: "Materials/GetRecords",
            data: "{'currentPage':1,'pageSize':5}",

Any suggestion....

EDIT:
I am calling a function from a view page,

 <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript">
        $(document).ready(function() {
            getMaterials(0);
          });
    </script>

and my function is,

function getMaterials(currentPage) {
    $.ajax({
        type:"POST",
        url: "Materials/GetRecords",
        data: "{'currentPage':" + (currentPage + 1) + ",'pageSize':5}",
        contentType: "application/json; charset=utf-8",
        cache: false,
        global: false,
        async: false,
        dataType: "json",
        success: function(data) {
            var divs = '';
            $("#ResultsDiv").empty();
            $.each(data.Results, function() {
                divs += '<div class="resultsdiv"><br /><span style="display: inline-block;width:150px;" class="resultName">' + this.Mat_Name + '</span><span class="resultfields" style="padding-left:10px;">Measurement :</span> <span class="resultfieldvalues">' + this.Mes_Name + '</span> <a href="/Materials/Delete/' + this.Id + '">Delete</a> <a href="/Materials/Details/' + this.Id + '">Details</a> <a href="/Materials/Edit/' + this.Id + '">Edit</a></div>';
            });
            alert("hai");
            $("#ResultsDiv").append(divs);
            $(".resultsdiv:even").addClass("resultseven");
            $(".resultsdiv").hover(function() {
                $(this).addClass("resultshover");
            }, function() {
                $(this).removeClass("resultshover");
            });
            $("#HfId").val("");
            $("#HfId").val(data.Count);
        }
    });
    return false;
}

on the view page load i see an alert saying [Object object].. Why this...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盛装女皇 2024-09-07 00:55:07

现在,您的数据是一个字符串。您可能希望它是一个对象,如下所示:

$.ajax({
        type:"POST",
        url: "Materials/GetRecords",
        data: {"currentPage": (currentPage + 1), "pageSize": 5},
        contentType: "application/json; charset=utf-8",
        cache: false,
        global: false,
        async: false,
        dataType: "json",
        success: function(data) {
            var divs = '';
            $("#ResultsDiv").empty();
            $.each(data.Results, function() {
                divs += '<div class="resultsdiv"><br /><span style="display: inline-block;width:150px;" class="resultName">' + this.Mat_Name + '</span><span class="resultfields" style="padding-left:10px;">Measurement :</span> <span class="resultfieldvalues">' + this.Mes_Name + '</span> <a href="/Materials/Delete/' + this.Id + '">Delete</a> <a href="/Materials/Details/' + this.Id + '">Details</a> <a href="/Materials/Edit/' + this.Id + '">Edit</a></div>';
            });
            alert("hai");
            $("#ResultsDiv").append(divs);
            $(".resultsdiv:even").addClass("resultseven");
            $(".resultsdiv").hover(function() {
                $(this).addClass("resultshover");
            }, function() {
                $(this).removeClass("resultshover");
            });
            $("#HfId").val("");
            $("#HfId").val(data.Count);
        }
    });

您通常会得到一个 JavaScript 对象([Object 对象])。您用它做什么取决于您的应用程序。然而,你是对的,没有警报(除了海),所以我不知道它来自哪里。尝试清除浏览器缓存。

Right now, your data is a string. You probably want it to be an object, like below:

$.ajax({
        type:"POST",
        url: "Materials/GetRecords",
        data: {"currentPage": (currentPage + 1), "pageSize": 5},
        contentType: "application/json; charset=utf-8",
        cache: false,
        global: false,
        async: false,
        dataType: "json",
        success: function(data) {
            var divs = '';
            $("#ResultsDiv").empty();
            $.each(data.Results, function() {
                divs += '<div class="resultsdiv"><br /><span style="display: inline-block;width:150px;" class="resultName">' + this.Mat_Name + '</span><span class="resultfields" style="padding-left:10px;">Measurement :</span> <span class="resultfieldvalues">' + this.Mes_Name + '</span> <a href="/Materials/Delete/' + this.Id + '">Delete</a> <a href="/Materials/Details/' + this.Id + '">Details</a> <a href="/Materials/Edit/' + this.Id + '">Edit</a></div>';
            });
            alert("hai");
            $("#ResultsDiv").append(divs);
            $(".resultsdiv:even").addClass("resultseven");
            $(".resultsdiv").hover(function() {
                $(this).addClass("resultshover");
            }, function() {
                $(this).removeClass("resultshover");
            });
            $("#HfId").val("");
            $("#HfId").val(data.Count);
        }
    });

It is normal for you to get a JavaScript object back (the [Object object]). What you do with this depends on your application. However, you're right there's no alert (except the hai), so I don't know where it's coming from. Try clearing your browser cache.

人间☆小暴躁 2024-09-07 00:55:07

我想你想要:

$.ajax({
        type:"POST",
        url: "Materials/GetRecords",
        data: {'currentPage':1,'pageSize':5},

I think you want:

$.ajax({
        type:"POST",
        url: "Materials/GetRecords",
        data: {'currentPage':1,'pageSize':5},
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文