0

我将值传递给 WCF 服务中的数组元素。我在这里使用 jQuery。

我的职能是:

在 Service.cs 中:

public class User
{

    Dictionary<int, string> users = null;
    public User()
    {
        users = new Dictionary<int, string>();
        users.Add(1, "apple");
        users.Add(2, "orange");
        users.Add(3, "lemon");
        users.Add(4, "grape");
    }

    public string[] GetUser(int Id)
    {
        var user = from u in users
                   where u.Key == Id
                   select u.Value;

        return user.ToArray<string>();
    }

在 jQuery 中:

function CallService() {
             $.ajax({
                 type: Type, 
                 url: Url, 
                 data: Data, 
                 contentType: ContentType,
                 dataType: DataType, 
                 processdata: ProcessData, 
                 success: function(msg) {
                     ServiceSucceeded(msg);
                 },
                 error: ServiceFailed
             });
         }

我这样声明的第一个函数:

 function WCFJSON() {

             var uesrid = "2";
             Type = "POST";
             Url = "Service.svc/GetUser";
             Data = '{"Id": "' + uesrid + '"}';
             ContentType = "application/json; charset=utf-8";
             DataType = "json"; ProcessData = true; 

             CallService();

         }

我调用的这个函数如下:

 $(document).ready(
         function() {
         WCFJSON();
         }
         );

我的第二个功能是:

function temp()
         {
         //var id=parseInt($('#txtinput').val();
         var id="5";
         Type="POST";
         Url="Srvice.svc/GetUser";
         Data='{"Id":"'+id+'"}';
         ContentType="application/json;charset=utf-8";
         DataType="json";ProcessData=true;
        CallService();

         }

我通过点击客户端按钮调用了这个函数,如下所示:

<asp:Button ID="btnsumbit" runat="server" Text="submit" OnClientClick ="temp();"  />

我的问题是当我运行这个程序时,我只得到了第一个函数(“WCFJSON();”)的结果,“temp();”的其他函数 我没有得到任何结果。我不知道我错过了什么?谁能解决这个问题?

4

1 回答 1

0

在此处输入图像描述 数据 = '{"Id": "' + uesrid + '"}';

应该:

Data = {"Id": uesrid};

这应该是一个对象,并将$.ajax对其进行序列化。

在 temp() 中,更改:

Data='{"Id":"'+id+'"}';

到:

Data={"Id": id };
于 2012-10-13T05:44:10.320 回答