调用一个WebMethod通过字典<字符串,字符串>作为参数 [英] Call a WebMethod passing Dictionary<string, string> as parameter

查看:147
本文介绍了调用一个WebMethod通过字典<字符串,字符串>作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图简化从我的WebMethod层返回数据到客户端,并重新present该组参数从客户端在词典&LT到来的过程中,字符串,字符串> 做这样的事情:

I am trying to streamline the process of returning the data from my WebMethod layer to the client and represent the set of parameters in coming from the client in a Dictionary<string,string> to do something like this:

    [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static override ResultObject<List<PatientInfo>> GetResults(Dictionary<string, string> query)
    {
        ResultObject<List<PatientInfo>> resultObject = null;

        if (!query.ContainsKey("finValue")) 
        {
            resultObject = new ResultObject<List<PatientInfo>>("Missing finValue parameter from the query");
        }

        string finValue = query["finValue"];

        if(finValue == null)
        {
            resultObject = new ResultObject<List<PatientInfo>>("Missing finValue parameter value from the query");
        }

        var patientData =  GetPatientsByFin(finValue);
        resultObject = new ResultObject<List<PatientInfo>>(patientData);
        return resultObject;

    }
}

我的问题是:如何传递和反序列化的词典参数

My question is: how do I pass and de-serialize the Dictionary parameter?

推荐答案

要通过一本字典,你必须使用一个WebService。

To pass a dictionary, you have to use a WebService.

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class TestService : System.Web.Services.WebService
{
    [WebMethod]
    public String PostBack(Dictionary<string, string> values)
    {
        //You should have your values now...
        return "Got it!";
    }
}

然后,当你想将它命名,你可以通过这样的事情。不知道,如果你使用jQuery,但这里的使用jQuery的Ajax方法的例子。

Then when you want to call it, you can pass something like this. Not sure if you're using jQuery, but here's an example using jQuery's ajax method.

var valueObject = {};
valueObject['key1'] = "value1";
valueObject['secondKey'] = "secondValue";
valueObject['keyThree'] = "3rdValue";

$.ajax({
    url: 'TestService.asmx/PostBack',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({ values: valueObject }),
    success: function (data) {
        alert(data);
    },
    error: function (jqXHR) {
        console.log(jqXHR);
    }
});

这篇关于调用一个WebMethod通过字典&LT;字符串,字符串&GT;作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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