2017-03-10 24 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 
}