0

I need to check the status code in vuejs, whether is it 200 or else. Here is my code, but i have errors.

methods:{
  userSearch(){
    let searchUrl = dataUrl+this.usersearch;
            
                fetch(searchUrl).then(statusCode => {
                    if(statusCode == 200){
                        message = "Not Available"
                        $("#availability").innerHTML = message
                    }
                    else {
                        message = "Available"
                        $("#availability").innerHTML = message
            }})

this should return in my p element with id="availability" whether the user is available or not, depending on the status code. I am calling this method in the input field on enter.

1
  • 2
    fetch resolves to a Response object, not just a status code… Commented May 31, 2022 at 13:15

1 Answer 1

0

As @deceze pointed out, the fetch function resolves to a Response Object.

You can see here how to properly handle the HTTP status of the response.

But, for your code, it should be something like this:

userSearch() {
    let searchUrl = dataUrl+this.usersearch;          
    fetch(searchUrl).then(response => {
        if(response.status == 200){
            message = "Not Available"
            $("#availability").innerHTML = message
        }
        else {
            message = "Available"
            $("#availability").innerHTML = message
        }
    })
}

As just part of the code was provided, there might be other causes for the error, that I cannot see with just this snippet.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.