locked
Connect WCF webservice using Javascript

    Question

  • Hi,

    I am using belowcode to connect to WCF servcie from Javascript (Metro style apps). Please suggest me i fi did it wrong.

    function callWCF() {
            WinJS.xhr({
                type: "POST",
                url: "http://....../Sample.svc/GetAuthorizedReportScopes",
                headers: { "Content-type": "application/x-www-form-urlencoded" }
            }).then(
            function (request) {
                var data = window.JSON.parse(request.responseText);
                var item = {
                    title: "Account Page",
                    content: "",
                    backgroundColor: 'rgba(25,50,200,1)'
                }
            },
            function (request) {
                goError(request.status);
            }
            );
        }

     

    Monday, January 30, 2012 4:54 AM

All replies

  • Hi M,

    Search this forum for WCF.  You will see lots of posts.

    This thread should help too: http://social.msdn.microsoft.com/Forums/en-US/winappswithhtml5/thread/fbe0de8a-3bc6-443a-8188-f67465797cd9

    -Jeff


    Jeff Sanders (MSFT)
    Monday, January 30, 2012 4:34 PM
    Moderator
  • Hi Jeff,

     

    Thanks, but for me Add service reference is not coming. I am using Javacript - Grid View application template

    Wednesday, February 1, 2012 7:27 AM
  • Hi M,

    The easiest way would be to create a Hybrid app using a C# WinMD component (that you create) to access the web service.

    Your JavaScript implementation has problems.  You are not sending a body for example.  You can definately do a call from JavaScript, but you need to understand what you need to send to the WebService.  You could use Fiddler (http://fiddler2.com) and inspect a successful call to the webservice and duplicate that in your JavaScript.  You could also understand from the WebService what you need to send (I think the asmx page will show you what a sample POST for the service is).  Remember you are simply using http traffic to make the call so you just need to duplicate what your service needs in your JavaScript.

    -Jeff


    Jeff Sanders (MSFT)
    Wednesday, February 1, 2012 1:16 PM
    Moderator
  • Hi Jeff,

     

    Thanks for your reply. as per my understanding we can pass data, type, username, password, headers and url by using Xhr. If I pass all the variables as below is it connect to WCF webservice and returns the data? If not please explain me the sample how to connect to WCF using Javascript with the same application template. WCF webservice is hosted in other machine.

     

    var data = '{"managerAlias":"danper", "Level":"0"}';
            var type = "POST";
            var url="http://......./PMService.svc/GetEmployeeListTemp";
            var headers = { "Content-type": "application/json; charset=utf-8" };

            WinJS.xhr({
                type: type,
                url: url,
                headers: headers,
                data: data
                }).then(function (result) {
                    ServiceSucceeded(result);
                }, function (result) {
                    ServiceFailed(result);
                });

            function ServiceFailed(result) {
                content.innerText = result.status;
            }
            function ServiceSucceeded(response) {
                content.innerText = response.GetEmployeeListTempResult;
            }
            } 

     

     

    Please help me on this issue

    Thursday, February 2, 2012 1:28 PM
  • You need to pass Data exactly like the webservice requires.  You can look at a success POST using fiddler (http://fiddler.com) or similar network inspection tools and then construct your data and headers exactly like that in your javascript code.  What this looks like depends on your service.  There is no standard way to do this.  Many webservices also support json.  If this is your webservice you would find it easier to do that.

    Have your WebService support JSON: http://channel9.msdn.com/Shows/The+EndPoint/Programming-JSON-with-WCF-in-NET-Framework-35


    Jeff Sanders (MSFT)


    Thursday, February 9, 2012 9:00 PM
    Moderator