簡體   English   中英

空響應正文與ajax請求

[英]Empty response body with ajax request

我正在將用戶名和密碼發布到龍卷風服務器。 如果憑據有效,則返回cookie和簡單的“ OK”文本響應。

這適用於普通的Web客戶端,但是當我嘗試使用Ajax進行操作時,響應主體為空。 Chrome出於某種原因在請求完成之前取消了該請求,即使它收到200狀態,Firefox也將其突出顯示為紅色。 我正在嘗試找出正在發生的事情。

我已經設置了CORS標頭,並且可以看到OPTIONS請求正在返回有效數據(chrome不抱怨它)。

這是我的龍卷風服務器:

class LoginHandler(BaseMixin, tornado.web.RequestHandler):
    @gen.coroutine
    def options(self, *args, **kwargs):
        """
        Return CORS headers
        """
        self.set_header('Access-Control-Allow-Headers', 'origin, content-type, accept, authorization')
        self.set_header('Access-Control-Allow-Max-Age', 21600)
        self.set_header('Access-Control-Allow-Methods', 'HEAD, OPTIONS, GET, POST')
        self.set_header('Access-Control-Allow-Origin', '*')

    @gen.coroutine
    def post(self, *args, **kwargs):
        """
        Validates the user's ID and token
        """
        try:
            # ... perform checks - all OK, so then:
            self.set_secure_cookie(
                    "auth", urllib.urlencode({'user_id': self.user_id,
                                              'token': user['token']}))
            self.set_header("Content-Type", "text/plain")
            self.finish("OK")
            return

內容長度已正確設置,但是由於某些原因,firefox或chrome無法顯示響應正文。

我以某種方式認為這與CORS有關,因為達到此端點的行為測試全部通過了(並且他們斷言響應主體包含“ OK”)。

我正在發出請求(使用angularjs),如下所示:

postData = new Array('user_id=' + id, 'token=' + token);

$http.post(url, postData.join('&'), {headers: {'Content-type': 'application/x-www-form-urlencoded'}})
   .success(authSuccessHandler)
   .error(authErrorHandler);

在chrome中,它會立即輸入錯誤回調,並在開發工具的網絡標簽中將請求標記為“待處理”,然后“取消”。

有誰知道我在做什么錯?

問題在於,CORS標頭僅在OPTIONS端點上,而不在POST上。 我也將它們添加到POST,現在一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM