locked
Calling WCF Service using JQuery RRS feed

  • Question

  • User1216489373 posted

    Hi Experts,

    I am not able to get Data from my WCF service while running the JQuery script, while when I run the same script for my webservice after chaning the URL I am getting the data. My WCF service is executing perfectly. The binding used is default basicHttpBinding. My jquery code for WCF is  as follows:-

                    function getEmployeeWCF() {
                        $.ajax({
                            type: "POST",
                            url: "http://localhost:1314/Service1.svc/GetData",
                            //data: "{}",
                            data: "{'Id':'" + $('#txt1').val() + "'}",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function (response) {
                                var employee = response.d;
                                $('#output').empty();
                                        $.each(employee, function (index, emp) {
                                            $('#output').append('<p></strong><br /> Id: ' +
                                                    emp.Id + '<br />Name: ' +
                                                    emp.Name + '<br />Salary: £' +
                                                    emp.Salary + '<br />Department: ' +
                                                    emp.Department + '</p>');
                                            });
    
                                    },
                            failure: function (msg) {
                                $('#output').text(msg);
                            }
                        });
                    }

    Please Help,

    Thanks,
    Rahul

    Monday, May 12, 2014 12:35 PM

Answers

  • User-417640953 posted

    Hi Rahul_agg11,

    Thank you post the issue to asp.net forum.

    type: "POST",

    url: "http://localhost:1314/Service1.svc/GetData",

    Based on your description and code provided, I see you want to call the wcf service using jquery cross domain.

    For this issue, I suggest note below points.

    # Make sure your svc service works as rest service, add Invoke attribute on the service method like below.

        [OperationContract]
        [WebInvoke(Method = "POST", 
         BodyStyle = WebMessageBodyStyle.Wrapped, 
         ResponseFormat = WebMessageFormat.Json)]
        string[] GetUser(string Id);
    

    # Enable your service can be consumed by config the service as web programing like below.

     <endpointBehaviors>
        <behavior name="EndpBehavior">
         <webHttp/>
        </behavior>
       </endpointBehaviors>
    

    # Config your service can be consumed cros domain like below.

    <httpProtocol>
    <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
    </customHeaders>
    </httpProtocol>

    For complete example, please follow below article.

    http://www.dotnet-tricks.com/Tutorial/wcf/X8QN260412-Calling-Cross-Domain-WCF-Service-using-Jquery.html

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 13, 2014 10:57 AM