我在我的代码可观察对象(订阅)中使用的是角度对象。 当请求的响应为200 ok时,它仍然转到subscribe(第二个参数)的错误行。
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.";
}
}
和角度代码:
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时,它仍然转到(err)行,该行将this.res初始化为true,而不是执行第一个表达式。
在浏览器中,当我打开f12并转到“网络”选项卡时,我看到请求已返回“ 5分钟已过去”->然后,问题一定出在打字稿中。
*重要说明:* 在前2分钟进入(res)语句。之后没有 还有关于程序的另一个问题: 为什么当我使用普通的chrome且没有不安全的chrome时,chrome不会创建JSESSIONID并仍然让我登录。
答案 0 :(得分:0)
首先传递一个空元素以订阅函数,然后将自定义函数用作第二个参数并进行检查,如下所示
subscribe((),(res) => {this.res = true; this.err = false ;this.code = res;