【发布时间】:2013-02-06 22:12:56
【问题描述】:
我有以下打字稿实现:
/// <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);
});
});
Javascript 中的输出是这样的:
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);
});
});
问题是它不断进入fail方法并返回status = 0和statusText = error。 我用Fiddler看调用是否完成,fiddler收到响应没有问题。
我怎样才能让这个东西正常工作?
【问题讨论】:
-
这不太可能是 TypeScript 问题。您可以尝试添加 JavaScript 标记,并发布 TS 编译器的 JS 输出,因为这可能有助于我们诊断问题。
-
@JcFx 我用额外的信息更新了我的问题。
-
我认为这更容易。我自己看不出 JS 有什么问题,但这里还有其他人可能有更多的 JQuery 知识。我唯一会做的是
console.logthat.serviceUrlgetNotifications中的that.serviceUrl,以确保它是正确的,并记录整个服务器响应,或者将调试器停在那里看看问题可能是什么 - 但你可以以前都做得很好。 -
@JcFx 我已经尝试了您建议的方法,但仍然没有运气。我真的希望有人可以帮助我。
-
Rob,您能否确认您在 Fiddler 中看到的是请求,而不仅仅是之前 CORS 用来检查服务器是否愿意发出实际请求的
OPTIONS调用?
标签: javascript jquery ajax typescript