I am calling an HTTP POST Request, the response is JSON data.
POST Request:
const postXHR = new XMLHttpRequest();
postXHR.open('POST', postOptions);
postXHR.setRequestHeader("Accept", "text/json");
postXHR.setRequestHeader("Content-Type", "text/json");
postXHR.onreadystatechange = function() {
const response = postXHR.response;
if (response.response) {
console.log(postXHR.status);
}
createNewOptionValues(response);
}
postXHR.send('{"optionName":"optionName46", "platformName":"platformName46","dotDigitalId":3,"googleId":4}');
POST Response/JSON Data:
{
"data": {
"rooftopGoogleOptionId": 99,
"googleId": 4,
"dotDigitalId": 3,
"optionName": "optionName46",
"optionValue": null,
"platformName": "platformName46",
"googleAccount": null,
"dotDigitalAccount": null,
"updatedBy": null,
"updatedAt": null,
"createdBy": "root",
},
"status": 201,
"message": "success"
}
The createNewOptionValue function should log the value of a property within the response.
function createNewOptionValues(obj){
console.log(obj.googleId);
console.log(obj['googleId']);
}
Yet, the output is undefined, when using console.log(obj), the response does show.
JSON.parse()to conver the JSON to an object.JSON.parse()works.JSON.parse(obj).