Witam
Próbuję zrobić fetch() w JS a mój serwerek odsyła co trzeba wiele razy ale nie trafia to do internetu zwiększając porty
powinno być - otrzymane GET, odesłane response, otrzymane HTTP, odesłany "<html><body>responseContent</body></html>" i finito
Jest komunikat "fetch() failed loading GET"
Connection accepted from 100.82.15.215:63475
//preflight
Otrzymane-------GET / HTTP/1.1
Host: 100.82.15.215:8080
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.57
Accept: */*
Origin: http://infinityhost.ct8.pl
Referer: http://infinityhost.ct8.pl/
Accept-Encoding: gzip, deflate
Accept-Language: pl,en;q=0.9,en-GB;q=0.8,en-US;q=0.7
------
wchodzimy w G
Wysylanie..
Message sent to port 8080: HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: *
Access-Control-Allow-Headers: *
Content-Type: text/html
responseContent
Connection accepted from 100.82.15.215:63832
Otrzymane-------HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: *
Access-Control-Allow-Headers: *
Content-Type: text/html
responseContent) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.57
Accept: */*
Origin: http://infinityhost.ct8.pl
Referer: http://infinityhost.ct8.pl/
Accept-Encoding: gzip, deflate
Accept-Language: pl,en;q=0.9,en-GB;q=0.8,en-US;q=0.7
------
wchodzimy w H
Wysylanie..
Message sent to port 8080: HTTP/1.1 200 OK
Content-Type: text/html
<html><body>responseContent</body></html>
Connection accepted from 100.82.15.215:63834
//i tutaj już tylko otrzymuje HTTP i wysyla ale nic nie dochodzi do internetu (porty się zwiększają co 2)
fetch wygląda tak
<html>
<body>
<script>
const url = 'http://100.82.15.215:8080/';
fetch(url)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('application/json')) {
return response.json(); // Parse response body as JSON
} else {
return response.text(); // Parse response body as text
}
})
.then(data => {
// Handle the parsed data
alert(data);
console.log('Data received:', data);
})
.catch(error => {
// Handle errors
console.error('Error:', error.message);
});
</script>
</body>
</html>
Pętla wygląda tak
int rozmiar=recv(clientSocket, bar, sizeof(bar), 0);
if(rozmiar>0){ Sleep(1500);
printf("\nOtrzymane-------%s------\n",bar);
if(bar[0]=='G'){
printf("wchodzimy w G\n");
char response[500];
strcpy(response, "HTTP/1.1 200 OK\r\n");
strcat(response, "Access-Control-Allow-Origin: *\r\n"); // Allow requests from any origin
strcat(response, "Access-Control-Allow-Methods: *\r\n");
strcat(response, "Access-Control-Allow-Headers: *\r\n");
strcat(response, "Content-Type: text/html\r\n\r\n");
strcat(response, "responseContent");
// Send the response to the client
sendMessage(8080, response);
}
if(bar[0]=='H'){
printf("wchodzimy w H\n");
char response[500];
strcpy(response, "HTTP/1.1 200 OK\r\n");
strcat(response, "Content-Type: text/html\r\n\r\n");
strcat(response, "<html><body>responseContent</body></html>\0");
sendMessage(8080, response);
}
if(bar[0]=='u'){
printf("wchodzimy w u\n");
elems[bar[1]-48]=bar[2];
printf("Updating to %s - %d \n",elems,bar[2]-48);
sendMessage(7000,elems);
sendMessage(7001,elems);
sendMessage(7002,elems);
}}
}