从.aspx向jquery ajax获取数据 [英] Getting data from .aspx to jquery ajax

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

问题描述

我是jquery的新手,我有一个问题: 我有两个.aspx文件:其中一个包含脚本

I'm novice in jquery and I have one problem: I have two .aspx files: one of them contain script

<script type ="text/javascript">
    $(document).ready(function () {
        var schemaName = GetURLParameters('schemaName');
        var key = GetURLParameters('key');
        $.post("dataloader.aspx", {
            name: schemaName,
            key: key
        });
    });
</script>

将参数发送到其他页面"dataloader.aspx".这是"dataloader.aspx.cs"代码:

which send parameters to other page, "dataloader.aspx". Here is "dataloader.aspx.cs" code:

protected void Page_Load(object sender, EventArgs e)
        {
            Response.ContentType = "application/json";
            var schemaName = Request.Form["name"];
            var key = Request.Form["key"];

            Loader loader = ConnectionManager.getLoader();
            Dictionary<string, string> name_value = new Dictionary<string, string>();
            if (!string.IsNullOrEmpty(schemaName))
            {
                var schema = loader.GetSchema(schemaName);
                var qcontext = new SimpleLoader.BOService.QueryContext();
                qcontext.InitQueryContext();

                var element = loader.GetObjectByKey(schema, key);
                var viselems = element._Schema.GetVisibleElems(); 
                var cardElems = viselems.Where(x => !(x is SchemaElemDetail)).ToList();

                foreach (var elem in cardElems)
                {
                    var value = (element.GetValue(elem.Name) ?? "").ToString();
                    if (!string.IsNullOrEmpty(value))
                    {
                        name_value.Add(elem.Name, value);
                    }
                }
                Response.Write(name_value);
                Response.Flush();
                Response.End();
            }

        }

如您所见,我正在向字典中添加一些数据.我想通过jQuery将这本词典发送到"clientcard.aspx"客户端,但是我不知道如何...您能帮我吗?我将非常感谢.

As you can see, I,m adding some data to dictionary. I want to send this dictionary to "clientcard.aspx" client side by jQuery, but i don't know how...Can you help me?? I'll be very grateful.

推荐答案

一种方法是在dataloader.aspx中调用Web方法.假设函数的名称为getNameValue,则在aspx页面中,您将具有如下所示的网络方法:(您基本上会将代码从Page_Load事件转移到该方法)

A way would be to call a webmethod in dataloader.aspx. Assuming your function's name would be getNameValue, in your aspx page, you'd have a webmethod like this : (You'd basically transfer the code from Page_Load event to this)

[System.Web.Services.WebMethod]
public static Dictionary<string, string> getNameValue(string name, string keyN) 
{
  var schemaName = name;
  var key = keyN;

  Loader loader = ConnectionManager.getLoader();
  Dictionary<string, string> name_value = new Dictionary<string, string>();
  if (!string.IsNullOrEmpty(schemaName))
  {
    var schema = loader.GetSchema(schemaName);
    var qcontext = new SimpleLoader.BOService.QueryContext();
    qcontext.InitQueryContext();

    var element = loader.GetObjectByKey(schema, key);
    var viselems = element._Schema.GetVisibleElems(); 
    var cardElems = viselems.Where(x => !(x is SchemaElemDetail)).ToList();

    foreach (var elem in cardElems)
    {
      var value = (element.GetValue(elem.Name) ?? "").ToString();
      if (!string.IsNullOrEmpty(value))
      {
         name_value.Add(elem.Name, value);
      }
    }    
  }
  return name_value; //will be automatically serialised to JSON because of the dataType specification in ajax call.
}

您可以像这样在ready中的jQuery中调用此函数:

You'd invoke this function in jQuery in ready like this :

  $(document).ready(function () {
        var schemaName = GetURLParameters('schemaName');
        var key = GetURLParameters('key');
        //just in case
        var data = JSON.stringify({
            name: schemaName,
            keyN: key
        });

        $.ajax({
            type: "POST",
            url: "dataloader.aspx/getNameValue",
            data: data,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: function (xhr, status, error) {
                var msg = JSON.parse(xhr.responseText);
                alert(msg.Message);
            }
          }).done(function (msg) {
            //msg.d will contain your dictionary
          });
  });

使用此方法更好的原因是代码变得可重用.在当前设置中,如果要获取name_value词典,则必须重新加载aspx页面.现在,您需要做的就是调用此方法.

The reason its better to use this method is that code becomes reusable. In your current set up, if you want to get the name_value dictionary you'd have to reload the aspx page. Now all you need to do is call this method.

希望这会有所帮助!

这篇关于从.aspx向jquery ajax获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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