I created 3 files (html, js and php) to call a restAPI from the js. But it is not working and I found zero clue on the web. It just does not work
Step 1 - HTML - for now, I use it to call the js script at the click on the button in this first moment I just try to call the js with the rest calling.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>
Web 2 Case - Abertura de Chamados
</title>
</head>
<body style="text-align:center;" id="body">
<!-- Button to send data -->
<button onclick="sendJSON()">Send JSON</button>
<!-- For printing result from server -->
<p class="result" style="color:green">
2 - Then I have index.js
function sendJSON(){
let result = document.querySelector('.result');
// Creating a XHR object
let xhr = new XMLHttpRequest();
let url = "**The endpoint url**";
// open a connection
xhr.open("POST", url, true);
// Set the request header i.e. which type of content you are sending
xhr.setRequestHeader("Content-Type", "application/json");
xht.setRequestHeader("Authorization","00Df0000003dGUH!AQUAQLpQJlcqfKpqZUW9KZjwubQi4YcV7IrfZrw_Y53X_adKMCBHVXzblySCHtYfwO5YLh1EBcUTrX7qxp9EkSjhKvBfzl4M");
// Create a state change callback
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
// Print received data from server
result.innerHTML = this.responseText;
}
};
// Converting JSON data to string
var data = JSON.stringify({"Fields":["requestDefinitionId": "a3Hf0000000lTNaEAM", "client": "0053j00000A7rWLAAZ"],["Answers":{"a3Df0000000qI63EAE":[ "Reclamação" ],"a3Df0000000qHvsEAE":[ "Solicitação de Serviço aberta por qualquer integração web","a3Df0000000qHwREAU":["Web"] ]}]});
// Sending data with the request
xhr.send(data);
}
I believe that my var data is not good. When I call the endpoint from postman, I wrote the body
{
"Fields": [
{
"Name": "requestDefinitionId",
"Value": "a3Hf0000000lTNaEAM"
},
{
"Name": "client",
"Value": "0053j00000A7rWLAAZ"
}
],
"Answers": [
{
"QuestionId": "a3Df0000000qI63EAE",
"Values": [ "Reclamação" ]
},
{
"QuestionId": "a3Df0000000qHvsEAE",
"Values": [ "Solicitação de Serviço aberta por qualquer integração web" ]
},
{
"QuestionId": "a3Df0000000qHwREAU",
"Values": ["Web"]
}
]
}
