2017-03-10 60 views
-1

這裏出了什麼問題?我100%確定我正在發送一個HTTP POST請求,但不知何故OR操作符不能按我所期望的那樣工作。在第一個例子中,服務器返回405,在第二個例子中代碼繼續執行。||操作員不按預期方式工作

不工作:

if req.Method != http.MethodPost || req.Method != http.MethodDelete { 
    http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) 
    return 
} 

工作:

if req.Method != http.MethodPost { 
    http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) 
    return 
} 
+4

看來你需要使用「&&」 - 你的病情始終爲TRUE – VladimirM

+0

@ tom12e - CONSOLE.LOG您的REG對象,看看Method屬性是什麼 – Phil

回答

2

(不是)OR(沒有別的互斥)總是將是真實的,不是嗎?

如果是方法後,它不會被刪除,反之亦然,你可能想要& &?

0

就像肯尼格蘭特說的,你可能想要思考邏輯。也許,這就是你的意思:

// only allow POST or DELETE 
if req.Method != http.MethodPost && req.Method != http.MethodDelete { 
    http.Error(res, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) 
    return 
}