Selçuklu Ebrar
Selçuklu Ebrar

Reputation: 2279

Using an ASP.Net class object (asp.net project is WCF service) from ajax (jquery)

I have a WCF service which sends string lists from client(jquery web pages).The wcf project runs perfect in visual studio when I run it with F5,a window is opening and I'm clicking Invoke after I can see the list object under the Invoke.I will using my list datas in jquery so I'm using ajax to call my wcf service,all configuration settings are right. I'm using abc class which have a list veriable,and I have a function data which sets some data to that list. Here is my Service contract and Data contract form WCF(IService1.cs):

namespace kgms
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
        abc data(abc obj);
   }
   [DataContract]
        public class abc {
            [DataMember]
            public List<string> sval { get; set; }
            [DataMember]
            public List<string> kval { get; set; }
        }
}

And here is my data function info(Service1.svc.cs):

namespace kgms
{
   [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1 : IService1
    {
       public abc data(abc obj) {
               obj.sval.Add("asd");
               obj.sval.Add("sdf");
               obj.kval.Add("example");
               obj.kval.Add(":-)");
            return obj;
        }
    }
}

I will access from that ajax call:

$('#buton').click(function () {
Type = "POST";
            Url = "http://compname/sil/Service1.svc/data";
            ContentType = "application/json; charset=utf-8";
            DataType = "json"; var ProcessData = true;
            CallService();

        });
 function CallService() {
            $.ajax({
                type: Type,
                url: Url,
                data: Object,
                contentType: ContentType,
                dataType: DataType,
                processdata: ProcessData,
                success: function (msg) {
                    ServiceSucceeded(msg);
                },
                error: ServiceFailed
            });
        }
        function ServiceFailed(result) {
            alert("basarisiz");
            alert('Service call failed: ' + result.status + '' + result.statusText);
            Type = null; varUrl = null; Data = null; ContentType = null; DataType = null; ProcessData = null;
        }
        function ServiceSucceeded(result) {
            alert("good");
        }
    });

what parameter must I send with Data: Object Object is sample, what must write instead of Object. Because my WCF function is taking an abc class object, what can I do.

Upvotes: 0

Views: 520

Answers (1)

Jhonatas Kleinkauff
Jhonatas Kleinkauff

Reputation: 966

hope this help you.

 var abcCtor = function() {
     this.sval  = [];
     this.kval  = [];
 }

 var mainObj = function() {
    this.abc = new abcCtor();
 }

 var abcObj = new mainObj();
    abcObj.abc.sval.push('name');
    abcObj.abc.sval.push('age');

    abcObj.abc.kval.push('jhonatas');
    abcObj.abc.kval.push('00');

var jsonValue = JSON.stringify(abcObj);

Then, pass the jsonValue to the paramater Data, of $ajax.

EDIT:

Simple do:

  public abc data() {
               obj.sval.Add("asd");
               obj.sval.Add("sdf");
               obj.kval.Add("example");
               obj.kval.Add(":-)");
            return obj;
        }

And omit the Data parameter from $ajax

Upvotes: 1

Related Questions