简体   繁体   English

响应 200 ok 并转到错误行

[英]Response 200 ok and goes to the error line

i'm using in my code observable object (subscribe) in angular.我在我的代码中使用 angular 中的可观察 object(订阅)。 when the response of the request is 200 ok it still goes to the error line of the subscribe(the 2nd argument).当请求的响应为 200 ok 时,它仍然会转到订阅的错误行(第二个参数)。

Code in java (server side): 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.";
    }

}

and code in angular:和 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 })


  }

when the response is 200k it still goes to the (err) line which initialize this.res to true instead of do the first expression.当响应为 200k 时,它仍会转到将 this.res 初始化为 true 的 (err) 行,而不是执行第一个表达式。

In the browser when I open f12 and go to "network" tab i see the request back "5 minute already passed" -- > then, the problem must be in the typescript.在浏览器中,当我打开 f12 和 go 到“网络”选项卡时,我看到请求返回“5 分钟已经过去”--> 那么,问题一定出在 typescript 上。

* important note:* at the first 2 min it goes to the (res) statements. * 重要说明:*在前 2 分钟,它转到 (res) 语句。 after it no.在它之后没有。 and another question about the program: why when i use normal chrome and no unsecured one the chrome doesn't create JSESSIONID and still let me login.还有关于该程序的另一个问题:为什么当我使用普通 chrome 而没有不安全的 chrome 时,chrome 不会创建 JSESSIONID 并且仍然让我登录。

Pass an empty element to subscribe function first, and use your customize function as a second argument and check, like below传递一个空元素以首先订阅 function,然后使用您自定义的 function 作为第二个参数并检查,如下所示

subscribe((),(res) => {this.res = true; this.err = false ;this.code = res;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使服务器出错,ResponseBuilder 也会给出 200 OK HTTP 响应 - ResponseBuilder gives 200 OK HTTP response even for server error HTTP 405-200 OK响应上的方法不允许错误 - HTTP 405 - Method Not Allowed error on a 200 OK response 泽西Response.ok()不给200 OK - Jersey Response.ok() not giving 200 OK 为什么我的服务器响应状态代码是200而不是200 OK? - Why my server response status code is 200 instead of 200 ok? $ .ajax。 当我得到200 OK状态和数据作为响应时,调用了错误函数? - $.ajax . Error function is invoked, while i am getting 200 Ok status and data as response? URLConnection到mediaUrl失败,获取错误消息意外状态行:KitKat上的ICY 200 OK - URLConnection to mediaUrl failed, get error message Unexpected status line: ICY 200 OK on KitKat OkHttp“java.net.ProtocolException:意外状态行:nullHTTP / 1.1 200 OK”在使用相同连接的“204 NO-CONTENT”响应之后 - OkHttp “java.net.ProtocolException: Unexpected status line: nullHTTP/1.1 200 OK” after a “204 NO-CONTENT” response using same connection OpenRefine 的 API:意外响应:HTTP/1.1 200 ok - OpenRefine's API: Unexpected response: HTTP/1.1 200 ok ajax 问题 - firebug 中的 200 OK 但没有响应正文的红色消息 - ajax problem - 200 OK in firebug but red message with no response body HttpUrlConnection:从非 200OK 获取响应正文 - HttpUrlConnection: Get response body from non 200OK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM