Hi,
I would like to create a button on a web page to send POST Rest API command to the loraserver. I tried with XMLHttpRequest and fetch(). Both can’t go through.
I have tried with 3 methods. All can’t go through.
Inside the button function, this line $headers_jwt[] = ‘Content-Type: application/json’; has caused the webpage unable to load.
If I tried with the below in the button function, the webpage is loaded but with “Cross-Origin Request Blocked: The Same Origin Policy” Error in browser. I tried with enabling Access-Control-Allow-Origin , be it in the header or in the apache.conf file or in the 000-default.conf file (following this link, enable cross-origin resource sharing), still can’t work.
var data_jwt = {'email': 'admin', 'password': 'admin'};
var xhttp_jwt = new XMLHttpRequest();
xhttp_jwt.open('POST', 'http://192.168.1.38:8080/api/internal/login', true);
xhttp_jwt.setRequestHeader('Accept', 'application/json');
xhttp_jwt.setRequestHeader('Content-type', 'application/json');
xhttp_jwt.send(JSON.stringify(data_jwt));
alert(xhttp_jwt.responseText);
If I tried with the fetch() function inside the button function, I get Failed to load resource: the server responded with a status of 400 (Bad Request)
const response = fetch(‘http://192.168.1.38:8080/api/internal/login’, {
body: {'email': 'admin',
'password': 'admin'
},
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
mode: 'no-cors',
method: 'POST'
});
What did I do wrongly? Could you please guide me?
Hope to hear from you soon.
Regards,
Nelson