2

I have the following typescript implementation:

/// <reference path="jquery.d.ts" />
interface INotificationService {
    serviceUrl: string;
    getNotifications(onNotificationReceived: (notifications: string) => any): void;
}
module GRCcontrol.Messaging {
    export class Notifier implements INotificationService {
        private serviceUrl: string;
        constructor(serviceUrl: string) {
            this.serviceUrl = serviceUrl;
            jQuery.support.cors = true;
        }
        getNotifications(onNotificationReceived: (notifications: string) => any) {
            var that = this;
            $.getJSON(that.serviceUrl, {}, function (response) {
                alert(response);
            }).fail(function (err) {
                alert(err.statusText);
            });
        }
    }
}
$(document).ready(function () {
    var notifier = new GRCcontrol.Messaging.Notifier("http://clwebsvr01/wp7test/api/user/getemail/P6dNT1EFOKYPtHxdPWgng2lKyMg=");
    notifier.getNotifications(function (notifications) {
        alert(notifications);
    });
});

The output in Javascript is this:

var GRCcontrol;
(function (GRCcontrol) {
    (function (Messaging) {
        var Notifier = (function () {
            function Notifier(serviceUrl) {
                this.serviceUrl = serviceUrl;
                jQuery.support.cors = true;
            }
            Notifier.prototype.getNotifications = function (onNotificationReceived) {
                var that = this;
                $.getJSON(that.serviceUrl, {
                }, function (response) {
                    alert(response);
                }).fail(function (err) {
                    alert(err.statusText);
                });
            };
            return Notifier;
        })();
        Messaging.Notifier = Notifier;        
    })(GRCcontrol.Messaging || (GRCcontrol.Messaging = {}));
    var Messaging = GRCcontrol.Messaging;
})(GRCcontrol || (GRCcontrol = {}));
$(document).ready(function () {
    var notifier = new GRCcontrol.Messaging.Notifier("http://clwebsvr01/wp7test/api/user/getemail/P6dNT1EFOKYPtHxdPWgng2lKyMg=");
    notifier.getNotifications(function (notifications) {
        alert(notifications);
    });
});

The problem is that it keeps getting into the fail method and returns status = 0 and statusText = error. I used Fiddler to see if the call is done, and fiddler receives the response without problems.

How can i get this thing to work properly?

10
  • 1
    This is unlikely to be a TypeScript issue. You could try adding a JavaScript tag, and also posting the JS output from the TS compiler, as that might help us diagnose the issue. Commented Feb 21, 2013 at 14:50
  • @JcFx i have updated my question with extra info. Commented Feb 21, 2013 at 14:54
  • I think that makes it easier. I can't see anything wrong with the JS myself, but there are others on here with more JQuery knowledge who may be able to. The only thing I would do is console.log that.serviceUrl in getNotifications, to make sure it is correct, and also log the entire server response, or put a debugger stop there to see what the issue might be - but you may well have done all this previously. Commented Feb 21, 2013 at 14:59
  • @JcFx I already tried the things you suggested, but still no luck. I really hope somebody can help me with this. Commented Feb 21, 2013 at 15:58
  • 1
    Rob, can you confirm that what you see in Fiddler is the request, not just the preceding OPTIONS call that CORS uses to check the server is happy to make the actual request? Commented Feb 21, 2013 at 17:17

1 Answer 1

2

This does not appear to have been a TypeScript issue at all, but rather a problem with the JavaScript/JQuery attempt to make a cors enabled ajax call.

For good reason, cors doesn't allow blanket access to all cross-domain resources. See this SO question and answers for more details: Is it safe to use $.support.cors = true; in jQuery?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.