locked
Cannot process the message because the content type "application/json; charset=utf-8" was not the expected type "text/xml; charset=utf-8"; RRS feed

  • Question

  • User738642305 posted

    Hi

    I am get the above response when calling a WCF service via ajax json. My calling code is:

            <script type="text/javascript">
                $(document).ready(function () {
                    $.ajax
                ({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "http://localhost:90/WebServices/UserService.svc/Calculate",
                    data: "{}",
                    timeout: 10000,
                    dataType: "json",
                    success: function (response) {
                        alert(response)
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert(xhr.statusText);
                        alert(thrownError);
                    }
                });
                });
            </script>
    


    My service is:

        [ServiceContract]
        public interface IUserService
        {
            [OperationContract]
            [WebInvoke(Method = "POST",
                       BodyStyle = WebMessageBodyStyle.Wrapped,
                       ResponseFormat = WebMessageFormat.Json
             )]
            Answer Calculate();
        }
    
        [DataContract]
        public class Answer
        {
            [DataMember]
            public string answer { get; set; }
        }

    My method is :

        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
        [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
        public class UserService : IUserService
        {
            public Answer Calculate()
            {
                Answer answer = new Answer();
                answer.answer="Hello World";
                return answer;
            }
        }


    Any help will be appreciated !!!

    Friday, January 18, 2013 11:36 AM

Answers

  • User738642305 posted

    I have eventually solved the major problem, I needed to add 

    Factory="System.ServiceModel.Activation.WebServiceHostFactory"

    to my SVC markup. e.g.

    <%@ ServiceHost Language="C#" Debug="true" Service="QuiznetOnline.Web.UI.WebServices.LogService" 
    Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 6, 2013 12:40 PM

All replies

  • User-1662538993 posted

       contentType: "application/json; charset=utf-8",

    Try -

    contentType: "application/json;

    Without charset= utf-8 
    Friday, January 18, 2013 2:12 PM
  • User738642305 posted

    I tried that but that still didn't work, it gave me :

    Cannot process the message because the content type 'application/json;' was not the expected type 'text/xml; charset=utf-8'.



    Friday, January 18, 2013 2:26 PM
  • User-1662538993 posted

    So your error says that it was expected to get text/xml instead of json?

    Friday, January 18, 2013 3:01 PM
  • User738642305 posted

    Seems like it

    Friday, January 18, 2013 3:16 PM
  • User-1662538993 posted

    When you directly see your service in browser can you able to go to the help page and see the calculate method.

    Can you click that method and does says the request format in json or not?

    Friday, January 18, 2013 5:00 PM
  • User738642305 posted

    Strange when I startup the service directly it shows me:

    UserService Service
    
    
    You have created a service.
    
    To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax:
    
    
    
    svcutil.exe http://localhost:90/WebServices/UserService.svc?wsdl
    
    This will generate a configuration file and a code file that contains the client class. Add the two files to your client application and use the generated client class to call the Service. For example:
    
    
    C#
    
    class Test
    {
        static void Main()
        {
            UserServiceClient client = new UserServiceClient();
    
            // Use the 'client' variable to call operations on the service.
    
            // Always close the client.
            client.Close();
        }
    }
    
    
    
    Visual Basic
    
    Class Test
        Shared Sub Main()
            Dim client As UserServiceClient = New UserServiceClient()
            ' Use the 'client' variable to call operations on the service.
    
            ' Always close the client.
            client.Close()
        End Sub
    End Class
    



    Friday, January 18, 2013 5:15 PM
  • User-1658256289 posted

    What is the issue now?

    Monday, January 21, 2013 4:21 AM
  • User738642305 posted

    I have eventually solved the major problem, I needed to add 

    Factory="System.ServiceModel.Activation.WebServiceHostFactory"

    to my SVC markup. e.g.

    <%@ ServiceHost Language="C#" Debug="true" Service="QuiznetOnline.Web.UI.WebServices.LogService" 
    Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 6, 2013 12:40 PM