0

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

I don't will add some datas from jquery, I will only call successfully the data function
Then, why you are expecting parameters in your c# method? I will edit my answer. Look that you dont need expect any parameter in your method if you wont use them.
Because my data function is running only when a parameter is comming.
Ok. So you need to do one of the two things. Change your parameters in c# class, or send parameters in json format, in $ajax method.
but giving obj doesn't exist,I have tried this ways ,and I creted obj object in data method, but nothing,when I take obj from parameter of data it's running,but when I create obj on any where unlike parameter it isn't running

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.