Unable to get data from WCF Service

    General discussion

  • Hi All,

    I'm developing an windows store apps using javascript.In this i'm trying to bind a sample code to span but it is throwing error message (which i kept a message saying error for testing purpose). Which previously the same WCF service is used and getting data from it(no issues).But trying here in the url nothing binding.

    I'm having WCF service like below

    [WebGet(UriTemplate = "/GetAreaNames",RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    List<Area> GetAreaNames();

    and HTML page like below

        <meta charset="utf-8" />
        <!-- WinJS references -->
        <link href="//Microsoft.WinJS.1.0/css/ui-dark.css" rel="stylesheet" />
        <script src="//Microsoft.WinJS.1.0/js/base.js"></script>
        <script src="//Microsoft.WinJS.1.0/js/ui.js"></script>
        <!-- App1 references -->
        <link href="/css/default.css" rel="stylesheet" />
        <script src="/js/default.js"></script>
        <button id="ButtonAdd">Show Text</button> 
        <span id="SpanResult" /> 

    and in the javascript page

    // For an introduction to the Blank template, see the following documentation:
    // http://go.microsoft.com/fwlink/?LinkId=232509
    (function () {
        "use strict";
        WinJS.Binding.optimizeBindingReferences = true;
        var app = WinJS.Application;
        var activation = Windows.ApplicationModel.Activation;
        app.onactivated = function (args) {
            if (args.detail.kind === activation.ActivationKind.launch) {
                if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                } else {
                    // TODO: This application has been reactivated from suspension.
                    // Restore application state here.
        app.oncheckpoint = function (args) {
            // TODO: This application is about to be suspended. Save any state
            // that needs to persist across suspensions here. You might use the
            // WinJS.Application.sessionState object, which is automatically
            // saved and restored across suspension. If you need to complete an
            // asynchronous operation before your application is suspended, call
            // args.setPromise().
        function Calculator() 
            var ButtonToAdd = document.getElementById('ButtonAdd');
            ButtonToAdd.addEventListener('click', function () {
                var url = "http://localhost:3045/WindowsStoreAppService.svc/GetAreaNames";
                WinJS.xhr({ url: url }).then(function (r) {
                    var result = JSON.parse(r.responseText);
                    ResultSpan.innerHTML = result; 
                function error(request) {
                    var msg = new Windows.UI.Popups.MessageDialog("Error");

    Please anybody tell me where i'd doing wrong and please some provide any sample code is much appreciated.


    D Haraveer.

    Wednesday, June 26, 2013 12:55 PM

All replies

  • There are 2 issues I can see in your code:

    1.) Your Calculator function is setting the value of an undefined ResultSpan. Your HTML defines SpanResult.

    2.) You should really be calling the Calculator function after the WinJS.UI.processAll() completes. That will ensure that the entire UI has been processed after which your Calculator function can reference any UI element in your HTML markup.

    I am also assuming that just browsing to your WCF service using IE browser is returning you the JSON formatted output.



    @prashantphadke || Windows Store Developer Solutions #WSDevSol || Want more solutions? See our blog, Windows Store & Phone Developer Solutions

    Tuesday, July 2, 2013 12:53 AM