none
how to invoke WCF service use Jquery under this scenario? RRS feed

  • Question

  • I have a wcf service like this:

    [ServiceContract] public interface IViewManage { [OperationContract] [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] object GetObject(string id); }

        [DataContract]
        public class Person
        {
            [DataMember]
            public string Name
            {
                get;
                set;
            }
            [DataMember]
            public int Age
            {
                get;
                set;
            }
        }

            [ServiceKnownType(typeof(Person))]
            public object GetObject(string id)
            {
                object obj =new Person() { Name = "JL", Age = 30 };
                return obj;
            }

    and I want to invoke the method in asp.net like this:

            function sendAJAX(url, data, success) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json",
                    url: url,
                    data: data,
                    processData: false,
                    success: success,
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert("Error Occured!");
                    }
                });
            }
            function GetLoadData() {
                debugger;
                var data = {"id": "id1"};
                var serviceAddress = "http://localhost/X0.Service.Workflow.ViewManageService/ViewManageJson.svc";
                var url = serviceAddress + "/GetObject";
                sendAJAX(url, jsonStr, function (resultData) {
                    alert(11);
                });
            };

    but I can't get the result,the fiddler hint something like this:

    [Fiddler] ReadResponse() failed: The server did not return a response for this request.Server returned

    any help ,thanks

     
    Tuesday, December 31, 2013 6:47 AM

Answers

  • I have resolved the problem,but the reason is that the [ServiceKnownTypeAttribute] must be  use on the OperationContract.

    Thanks

    • Marked as answer by JL_Nostalgic Wednesday, January 1, 2014 8:11 AM
    Wednesday, January 1, 2014 7:56 AM

All replies

  • Hi,

    I saw that you have used the following:

    var url = serviceAddress + "/GetObject";
    But you did not set the UriTemplate, please try to check this:
     [OperationContract]
            [WebInvoke(Method = "POST", 
                UriTemplate = "/GetObject", 
                RequestFormat = WebMessageFormat.Json,
                ResponseFormat = WebMessageFormat.Json,
                BodyStyle = WebMessageBodyStyle.Wrapped)]
            object GetObject(string id);

    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, January 1, 2014 2:14 AM
    Moderator
  • I have resolved the problem,but the reason is that the [ServiceKnownTypeAttribute] must be  use on the OperationContract.

    Thanks

    • Marked as answer by JL_Nostalgic Wednesday, January 1, 2014 8:11 AM
    Wednesday, January 1, 2014 7:56 AM
  • Hi,

    I am very glad that you have solved your problem, please mark it as an answer.

    Thank you.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 1, 2014 8:01 AM
    Moderator