-1

I am having an issue with my code but I can't seem to figure it out. When I run LiveServer with Visual Studio the website loads but as soon as I click the "GET" button that I put in it throws "ERR_CONNECTION_REFUSED". I decided to dig a little deeper with inspect source and it pointed to this line of code:

xhr.send();

This is exactly how my professor is showing us how to do it and it works good for him so I don't really understand my issue. I've already tried disabling my ad blocker, dns flush, deselecting "use proxy for LAN". The browsers I've tested so far have been Chrome, Brave, and Edge to no success. Here is my code as well as my exact error for anyone who may be curious:

let url = "http://localhost:8080/api/";
$().ready(()=>{

    //GET Button
 $('#btn-get').click(()=>{
        $("#ajax-form").html ( getDeleteForm('get') )
        $("#go-get-delete").click(()=>{
            //get id from form
            let id = $("#form-get-delete #id").val();

            //create AJAX call
            var xhr = new XMLHttpRequest();
            xhr.open('GET',url + id);
            xhr.send();
            xhr.onreadystatechange = ()=>{
                if(xhr.readyState == 4 && xhr.status == 200){
                    //Convert data to JS object
                    let books = JSON.parse(xhr.responseText);
                    processResult(books,'GET');
                }
            }```


`GET http://localhost:8080/api/ net::ERR_CONNECTION_REFUSED`
s3lman
  • 21
  • 2
  • Have you checked the ports you are using for API are correctly configured? – Lae Dec 09 '20 at 02:28
  • Read the comments here https://stackoverflow.com/questions/61943994/api-ok-from-localhost-but-failed-with-ajax-err-connection-refused and check this out https://stackoverflow.com/questions/60158617/localhost-refuses-to-connect-err-connection-refused – react_or_angluar Dec 09 '20 at 02:31
  • what http server are you running on port 8080 on the local machine? – Bravo Dec 09 '20 at 02:38
  • @Bravo the server that I am running is 127.0.0.1 – s3lman Dec 09 '20 at 05:27
  • @Lae Yes from what I can see I have my ports correctly configured. The only port that is different is the port for the LiveServer which was automatically configured for port 5500 when installed. – s3lman Dec 09 '20 at 05:32
  • @s3lman - yes, I know localhost is 127.0.0.1 .... but what is the server (the software)? and what port is it using – Bravo Dec 09 '20 at 05:37
  • @quantumPuter I've seen both of those posts and tried what was suggested but it didn't work. – s3lman Dec 09 '20 at 05:38
  • By the way, it's odd you are using jQuery and XMLHttpRequest - usually those using jQuery use the jQuery *ajax* methods to make coding simpler – Bravo Dec 09 '20 at 05:44
  • @Bravo I don't really understand your question. My apologies this is the first time that I've ever worked with something like this so everything I'm learning is completely new to me. – s3lman Dec 09 '20 at 05:46
  • @Bravo Honestly I'm just following exactly what my professor is doing so I can get a better idea of what to do. I guess using both jQuery and XMLHttpRequest seems simpler to him. – s3lman Dec 09 '20 at 05:48
  • but what is the server (the software)? and what port is it using? – Bravo Dec 09 '20 at 05:55
  • @Bravo I'm guessing you mean the LiveServer extension that I am using. It's an extension that I downloaded off of Visual Studio Codes extension page. When I look at the settings it shows that it uses port 5500. – s3lman Dec 09 '20 at 06:01
  • which isn't the same as port 8080 – Bravo Dec 09 '20 at 06:30
  • @Bravo I went into the settings and changed the port to 8080 and It's not showing an error anymore which is a step in the right direction but now it wont show the table that I set up. When I search for new items I get an error "books.forEach" is not a function. I've just been following instructions left for me but it seems I am forgetting something. – s3lman Dec 09 '20 at 06:46
  • perhaps `JSON.parse(xhr.responseText)` isn't an Array - only you know what `xhr.responseText` is – Bravo Dec 09 '20 at 07:00
  • as for `This is exactly how my professor is showing us how to do it` - ask your professor if he's even heard of XMLHttpRequest Level 2 - I assume he's heard that internet explorer 9 was released? – Bravo Dec 09 '20 at 07:02

1 Answers1

0

@Bravo Thank you for your help I was able to find out why it wasn't functioning correctly. So changing the port on LiveServer to match the port in my code ended up solving the initial problem that I had. As for the function error I received I was able to solve that easily. I did not realize that I had Books.foreach written instead of Books.forEach. It also looks like I forgot to use the # for some of my processing functions.

s3lman
  • 21
  • 2