Home > Enterprise >  Javascript: ajax, access/parse response node/parameter/element
Javascript: ajax, access/parse response node/parameter/element

Time:09-01

I am using ajax to call a jssp page that will return a json response

  var getDetails = function(){
       event.preventDefault();
       var id  = document.getElementById("recipientIdM").value;             
       var id2 = document.getElementById("recipientIdD").value;
       var url = "/sch/recipientAjaxV1.jssp?id=" id "&id2=" id2;
       
       document.controller.setValue('/ctx/vars/id', id);       
       document.controller.setValue('/ctx/vars/id2', id2);  
       
       var request = $.ajax({
        type: 'GET',
        url: url,
        async: true,
        success: function(response) {
            //alert(JSON.stringify(response));
            $('#recipientData').trigger("reset");
            addToForm(response)                     
         }
      });
  
      }

Response

{
    "idM": 13249906,
    "cmOneIDM": 888888,
    "jurisdictionM": "SCPB - LON",
    "firstNameM": "David Dynamic",
    "lastNameM": "Test User",
    "emailM": "[email protected]",
    "error2": "WDB-200001 SQL statement 'SELECT   R0.iRecipientId, 'could not be executed.\n  Param(0)=132486762\nWDB-200011 The requested database record does not exist.\nCannot load document of type 'Recipients (nms:recipient)' satisfying condition '([/@id] = 132486762)'."
}

How can I access the response and check for any nodes that start with the name of "error" and assign them to a local variable?

CodePudding user response:

You can filter the keys of the response by whether they start with 'error' and then map those keys to the values associated with them:

const response = {
  "idM": 13249906,
  "cmOneIDM": 888888,
  "jurisdictionM": "SCPB - LON",
  "firstNameM": "David Dynamic",
  "lastNameM": "Test User",
  "emailM": "[email protected]",
  "error2": "WDB-200001 SQL statement 'SELECT   R0.iRecipientId, 'could not be executed.\n  Param(0)=132486762\nWDB-200011 The requested database record does not exist.\nCannot load document of type 'Recipients (nms:recipient)' satisfying condition '([/@id] = 132486762)'."
}

const errors = Object.keys(response)
  .filter(k => k.startsWith('error'))
  .map(k => response[k])

console.log(errors)

Or you could use reduce directly on the entries in the response object:

const response = {
  "idM": 13249906,
  "cmOneIDM": 888888,
  "jurisdictionM": "SCPB - LON",
  "firstNameM": "David Dynamic",
  "lastNameM": "Test User",
  "emailM": "[email protected]",
  "error2": "WDB-200001 SQL statement 'SELECT   R0.iRecipientId, 'could not be executed.\n  Param(0)=132486762\nWDB-200011 The requested database record does not exist.\nCannot load document of type 'Recipients (nms:recipient)' satisfying condition '([/@id] = 132486762)'."
}

const errors = Object.entries(response).reduce((acc, [k, v]) => {
  if (k.startsWith('error')) acc.push(v)
  return acc
}, [])

console.log(errors)

CodePudding user response:

Try this:

const errors = Object.entries(response).filter(function(key) {
  return key.startsWith('error')
})

console.log(errors)
  • Related