Javascript 从typescript调用webservice时出错
我有以下typescript实现:Javascript 从typescript调用webservice时出错,javascript,jquery,ajax,typescript,Javascript,Jquery,Ajax,Typescript,我有以下typescript实现: /// <reference path="jquery.d.ts" /> interface INotificationService { serviceUrl: string; getNotifications(onNotificationReceived: (notifications: string) => any): void; } module GRCcontrol.Messaging { export cl
/// <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);
});
});
问题是它一直进入fail方法并返回status=0和statusText=error。
我使用Fiddler查看调用是否完成,Fiddler会毫无问题地收到响应
我怎样才能使这东西正常工作 这似乎根本不是TypeScript问题,而是JavaScript/JQuery尝试进行启用了
cors
的ajax调用时出现的问题
出于充分的理由,
cors
不允许对所有跨域资源进行全面访问。有关更多详细信息,请参阅此问题和答案:这不太可能是打字脚本问题。您可以尝试添加一个JavaScript标记,还可以发布TS编译器的JS输出,因为这可能有助于我们诊断问题。@JcFx我已经用额外的信息更新了我的问题。我认为这会更容易。我自己看不出JS有什么问题,但这里还有其他人可能有更多的JQuery知识。我唯一要做的就是在getNotifications
中console.log
记录.serviceUrl
,以确保它是正确的,并记录整个服务器响应,或者在那里放置一个调试器以查看可能的问题-但您以前可能已经做了所有这些。@JcFx我已经尝试了您建议的方法,但还是没有运气。我真的希望有人能帮我。Rob,你能确认你在Fiddler中看到的是请求,而不仅仅是CORS用来检查服务器是否乐意发出实际请求的前面的选项调用吗?
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);
});
});