ajax jquery post方法 [英] ajax jquery post method

查看:78
本文介绍了ajax jquery post方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have ajax request to url DataProcessor.aspx like shown below ,how can i write asp.net c# code to extract the json data from request and display in DataProcessor.aspx page

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>");


            }
        });
    });

推荐答案

与其发布json并手动对其进行解析,另一种选择是创建脚本服务并使用该服务.这样做的好处是重量轻(您不必经历整个aspx页面生命周期),并且让.Net进行繁重的工作来将json解析为对象.

Rather than post the json and parse it manually, another option would be to create a script service and consume that service. This has the advantage of being lighter weight (you won' t have to to go through the whole aspx page lifecycle) and let's .Net do the heavy lifting for parsing the json into an object.

基本上,您只需要创建一个简单的Web方法即可,该方法带有两个参数,即ItemName,CategoryID和RecordID.将脚本服务装饰器或方法应用到方法,以让.Net知道您要通过JSON POST与之交互,然后将帖子的地址更改为"YourWebService.asmx/YourWebMethodName"

Basically you will just need to create a simple web method that takes two arguments, ItemName, CategoryID, RecordID. Apply the Script Service decorator or to the method to let .Net know you want to interact with it via JSON POSTs, and change the address of your post to "YourWebService.asmx/YourWebMethodName"

要处理显示的更新,请创建一个对象以从函数中返回,该对象包含要在页面上更新的数据并返回.在成功调用AJAX的情况下,处理返回值(上述函数中的"response"参数),并相应地更新显示内容(不了解更多有关如何更新或更新的内容,对此我无能为力).

To handle updates to the display, create an object to return from your function that contains the data you want to update on the page and return it. Process the return value (the 'response' argument in your function above) in the success case of your AJAX call and update the display accordingly (not knowing more about how or what you want to update, I can't really help further on that).

这篇关于ajax jquery post方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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