【问题标题】:ajax jquery post methodajax jquery post方法
【发布时间】:2010-08-24 09:37:27
【问题描述】:

我有 ajax 请求到 url DataProcessor.aspx 如下所示,我如何编写 asp.net c# 代码从请求中提取 json 数据并显示在 DataProcessor.aspx 页面中

var json = "{'ItemName':'" + escape(item.val()) + "','CategoryID':'" + category.val() + "','RecordID':'" + record.val() + "'}";
            alert(escape(item.val()));
            alert(category.val());
            alert(record.val());
            var ajaxPage = "DataProcessor.aspx?Save=1"; //this page is where data is to be retrieved and processed
            var options = {
                type: "POST",
                url: ajaxPage, 
                data: json,
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                async: false,
                success: function(response) {
                    //alert("success: " + response);
                },
                error: function(msg) { alert("failed: " + msg); }
            };

            //execute the ajax call and get a response
            var returnText = $.ajax(options).responseText;
            if (returnText == 1) {

                record.html(returnText);
                $("#divMsg").html("<font color=blue>Record saved successfully.</font>");
            }
            else {
                record.html(returnText);
                $("#divMsg").html("<font color=red>Record not saved successfully.</font>");


            }
        });
    });

【问题讨论】:

标签: asp.net json jquery


【解决方案1】:

与其发布 json 并手动解析它,另一种选择是创建一个脚本服务并使用该服务。这具有重量更轻的优点(您不必经历整个 aspx 页面生命周期),让 .Net 完成将 json 解析为对象的繁重工作。

基本上,您只需要创建一个简单的 Web 方法,该方法接受两个参数,ItemName、CategoryID、RecordID。应用脚本服务装饰器或方法让 .Net 知道您想通过 JSON POST 与其交互,并将您的帖子地址更改为“YourWebService.asmx/YourWebMethodName”

要处理对显示的更新,请创建一个对象以从您的函数返回,其中包含您要在页面上更新的数据并将其返回。在您的 AJAX 调用成功的情况下处理返回值(上面函数中的“响应”参数)并相应地更新显示(不知道更多关于您想要更新的方式或内容,我真的无能为力)。

【讨论】:

    猜你喜欢
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多