将服务器错误消息传递给客户端(使用Express和Backbone)
我一直在颠簸一下,有困难找出如何将服务器错误消息传递给客户端。
在我有(简化)的服务器上:
export function get(req: express.ExpressServerRequest, res: express.ExpressServerResponse) { res.statusCode = 500; res.send('CUSTOM ERROR MESSAGE'); }
在客户端:
public fetchObject(successF: Function, failF: Function): void { this.myObj = new MyObj(); this.myObj.fetch({ success: successF, error: failF }); } private failF(model, xhr, options): void { // Want to get access to "CUSTOM ERROR MESSAGE" }
xhr对象的responseText是空的,statusText总是“错误”。
有什么build议么? 谢谢!
find了解决办法。 定义一个类variables并捕获获取调用的返回值:
private xhr: XMLHttpRequest = null;
然后:
public fetchObject(successF: Function, failF: Function): void { this.myObj = new MyObj(); this.xhr = this.myObj.fetch({ success: successF, error: failF }); }
最后:
private failF(model, xhr, options): void { doSomething(this.xhr.responseText); }
this.xhr将包含reponseText(即“CUSTOM ERROR MESSAGE”)。 本地xhr仍然是一个空白string。
我仍然不确定为什么会这样,如果有人有一些洞察力,我会很感激。