tags:

views:

76

answers:

2

I am try to call my web service with ajax call but i am not get success to call web service with it please find below code

Web Method Which i am trying to call

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Collections.Generic;

/// <summary>
/// Summary description for Jasonexample
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class Jasonexample : System.Web.Services.WebService {

    public Jasonexample () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld(Dictionary<string,object>Input) {
        return "Hello World " + Input["FName"].ToString() + " " + Input["LName"].ToString();
    }
    [WebMethod]
    public string test ( )
    {
        return "test";
    }

}

My ajax call javascript

/// <reference path="jquery-1.2.3.min.js" />
$(document).ready(function() {
    dict = new Object();
    dict["FName"] = "Abhishek";
    dict["LName"] = "Hingu";
    $.ajax({
    type: "POST",
    url: "Jasonexample.asmx/HelloWorld",
    data: '[{"Key":"FName","Value":"Abhi"},{"Key":"LName","Value":"Hingu"}]',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
      alert("Hello");
      alert(msg);
    },
    fail:function(msg)
    {
        alert("fail");
    }
  });
});

Now any one can you tell me how to pass dictionary collection in ajax called

A: 

I solved this problem with this solution(I was using Microsoft Ajax)

On server side made method:

    [WebMethod]
    public string GetPersonsList ( int page, string filter, int ID, OrderedDictionary SelectParameters )
    {
        //code        
        return outData;
    }

And client call makes request with such POST parameters:

{"page":0,"filter":"","ID":-1,"SelectParameters":{"pClassId":1}}

So in your call you should make data variable like this:

data: '{"Input":{"FName":"Abhi","LName":"Hingu"}}'
Maxim
A: 

A good way to figure out how a particular collection is expected for deserialization is to return that collection from a test method and examine how JavaScriptSerializer has serialized it. For example, this method:

[WebMethod]
public Dictionary<string,object> HelloWorld() {
  Dictionary<string, object> d = new Dictionary<string, object>();

  d.Add("FName", "Dave");
  d.Add("LName", "Ward");

  return d;
}

Returns this JSON:

{"FName":"Dave","LName":"Ward"}

Try using this as your $.ajax() data parameter:

data: "{'FName':'Dave','LName':'Ward'}"

To avoid the ambiguity that comes with using a vague collection, like Dictionary, you might consider using a DTO class instead.

Dave Ward

related questions