[英]How can i get tornado error response in ember js action?
我在Ember.js中編寫了一個針對Tornado服務器搜索結果的搜索操作。 現在的問題是,當搜索未找到任何結果時,我使用tornado.web.HTTPError(statuscode)
引發錯誤:
Server.py:
=========
if not rows:
raise tornado.web.HTTPError(400)
我正在嘗試像這樣處理App.js文件中的錯誤:
App.js:
========
search: function() {
// the current value of the text field
var query = this.get('query');
data = $.ajax({
dataType: "json",
url: "/search?query=" + query,
async: false}).error(function(response) {
alert(response.responseText);
}).responseJSON;
在上面的代碼中,當Tornado引發錯誤時,將執行.error(response)
,但responseText
為空。
如何捕獲該錯誤並重定向到錯誤頁面?
問題不是來自EmberJS。 它在Ajax Request
。 您正在請求JSON的data-type
,但正在404
中發送HTML內容(可能類似於<html><title>400: Bad Request</title><body>400: Bad Request</body></html>
),解析后返回空值。 從AJAX請求中刪除數據類型為JSON或在服務器中編寫這樣的自定義處理程序,以JSON模式返回404錯誤。
class MyHandler(tornado.web.RequestHandler):
def get(self):
self.clear()
self.set_status(400)
self.finish("[]") //some JSON
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.