Home > Mobile >  how I get Value from object from Ajax
how I get Value from object from Ajax

Time:12-15

I have this Ajax Call in java script:

function getCamInfo(value) {
  var body = "TakeNewPic="   value;
  var req = new XMLHttpRequest();

  req.onreadystatechange = function() {
    if (this.readyState === 4 && this.status === 200) {
      var res = document.getElementById("parg");
      var outpt = JSON.parse(this.responseText);
      res.innerHTML = outpt;

      //console.log(outpt);
      /* var link = outpt.state[0];  
       var tex = document.getElementById("linkP");
       tex.value = link;*/
    }
  }

  req.open(
    "POST",
    "post_req.php",
    true,
  );

  req.setRequestHeader(
    "content-type",
    "application/x-www-form-urlencoded"
  );

  req.send(body);
}

I got this JSON Object in Response :

{"fingerprint":"FIG_0163","state":{"_apiVersion":2,"batteryLevel":0.78,"_batteryState":"disconnect","_cameraError":[],"_captureStatus":"idle","_capturedPictures":0,"_compositeShootingElapsedTime":0,"_function":"mySetting","_latestFileUrl":"http://192.168.254.124/files/90014a68423861503e031db848764600/100RICOH/R0011105.JPG","_mySettingChanged":false,"_pluginRunning":false,"_pluginWebServer":true,"_recordableTime":0,"_recordedTime":0,"storageUri":"http://192.168.254.124/files/90014a68423861503e031db848764600/"}}

How I can get only one Value from this Object for Example : only Url Picture:

"_latestFileUrl":"http://192.168.254.124/files/90014a68423861503e031db848764600/100RICOH/R0011105.JPG"

thanks so much

CodePudding user response:

try this

var latestFileUrl = jsonObject.state._latestFileUrl;

CodePudding user response:

Modify your res.innerHTML with the below one.

res.innerHTML = outpt.state._latestFileUrl;

CodePudding user response:

For a straightforward version:

const _fetch = () => Promise.resolve({
  "fingerprint": "FIG_0163",
  "state": {
    "_apiVersion": 2,
    "batteryLevel": 0.78,
    "_batteryState": "disconnect",
    "_cameraError": [],
    "_captureStatus": "idle",
    "_capturedPictures": 0,
    "_compositeShootingElapsedTime": 0,
    "_function": "mySetting",
    "_latestFileUrl": "http://192.168.254.124/files/90014a68423861503e031db848764600/100RICOH/R0011105.JPG",
    "_mySettingChanged": false,
    "_pluginRunning": false,
    "_pluginWebServer": true,
    "_recordableTime": 0,
    "_recordedTime": 0,
    "storageUri": "http://192.168.254.124/files/90014a68423861503e031db848764600/"
  }
})

_fetch()
  .then(x => x.state["_latestFileUrl"])
  .then(console.log);

For an overcomplicated (but interesting to me) version:

const _fetch = () => Promise.resolve({
  "fingerprint": "FIG_0163",
  "state": {
    "_apiVersion": 2,
    "batteryLevel": 0.78,
    "_batteryState": "disconnect",
    "_cameraError": [],
    "_captureStatus": "idle",
    "_capturedPictures": 0,
    "_compositeShootingElapsedTime": 0,
    "_function": "mySetting",
    "_latestFileUrl": "http://192.168.254.124/files/90014a68423861503e031db848764600/100RICOH/R0011105.JPG",
    "_mySettingChanged": false,
    "_pluginRunning": false,
    "_pluginWebServer": true,
    "_recordableTime": 0,
    "_recordedTime": 0,
    "storageUri": "http://192.168.254.124/files/90014a68423861503e031db848764600/"
  }
})

const compose = (...fns) =>
  fns.reduceRight((prevFn, nextFn) =>
    (...args) => nextFn(prevFn(...args)),
    value => value
  );

const prop = p => o => o[p];

const getFileUrlFromResponse = compose(prop("_latestFileUrl"), prop("state"));

_fetch()
  .then(getFileUrlFromResponse)
  .then(console.log);

  • Related