![](/img/trans.png)
[英]ResponseBuilder gives 200 OK HTTP response even for server error
[英]Response 200 ok and goes to the error line
我在我的代碼中使用 angular 中的可觀察 object(訂閱)。 當請求的響應為 200 ok 時,它仍然會轉到訂閱的錯誤行(第二個參數)。
java(服務器端)中的代碼:
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import zoolpon.project.entities.Client;
import zoolpon.project.entities.CustomLogin;
import zoolpon.project.exceptions.InvalidLoginException;
import zoolpon.project.repositories.ClientRepository;
import zoolpon.project.services.UserService;
@CrossOrigin("http://localhost:4200")
@RestController
@RequestMapping("systemProject/User")
@Validated
public class UserWebService {
@Autowired
UserService userService;
@Autowired
private HttpServletRequest request;
@Autowired
ClientRepository clientRepository;
@Autowired
Logger logger;
public CustomLogin getFacade() {
HttpSession session = request.getSession(false);
return (CustomLogin) session.getAttribute("FACADE");
}
@RequestMapping(path = "byCode/{phoneNumber}", method = RequestMethod.GET)
public String getCode(@PathVariable String phoneNumber) throws InvalidLoginException {
Client client = clientRepository.findByPhoneNumber(phoneNumber);
Date date = new Date();
String[] companies = getFacade().getCompany();
for (String string : companies) {
if (client.getCompany().equals(string)) {
if (client.getExpire_time().after(date)) {
return client.getCode();
}
return "5 minutes already passed.";
}
}
logger.info("Something went wrong , Invalid phone number.");
return "Something went wrong , Invalid phone number.";
}
}
和 angular 中的代碼:
this.MyHttpClient.get<any>("http://localhost:8080/systemProject/User/byCode/" + phoneNumber)
.subscribe((res) => {this.res = true; this.err = false ;this.code = res;
if(this.code == null) {
this.err = true;
}
},
(err) => { this.err = true; this.res = false })
}
當響應為 200k 時,它仍會轉到將 this.res 初始化為 true 的 (err) 行,而不是執行第一個表達式。
在瀏覽器中,當我打開 f12 和 go 到“網絡”選項卡時,我看到請求返回“5 分鍾已經過去”--> 那么,問題一定出在 typescript 上。
* 重要說明:*在前 2 分鍾,它轉到 (res) 語句。 在它之后沒有。 還有關於該程序的另一個問題:為什么當我使用普通 chrome 而沒有不安全的 chrome 時,chrome 不會創建 JSESSIONID 並且仍然讓我登錄。
傳遞一個空元素以首先訂閱 function,然后使用您自定義的 function 作為第二個參數並檢查,如下所示
subscribe((),(res) => {this.res = true; this.err = false ;this.code = res;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.