0

I am try to collect data and images from local server (Acquia Dev Desktop) using this Angular Js code

controller

var app = angular.module('App', []);
app.controller('Ctrl', function($scope, $http) {
     $scope.images = [];
    $http({
        method : "GET",
        url    : 'http://docroot.com.dd:8083/catalogue/11/images/json'
    }).then(function mySucces(response) {
        $scope.images = response.data;
    }, function myError(response) {
        $scope.images = response.statusText;
    });
});

Json

[{"image":" <a href=\"http:\/\/docroot.com.dd:8083\/sites\/docroot.com.dd\/files\/catalogues\/2016-09\/images\/Pty%20Prs.compressedjpg_Page1.jpg\">Property Press.compressedjpg_Page1.jpg<\/a>"}]

// i got out put like this : 

 <a href=\"http:\/\/docroot.com.dd:8083\/sites\/docroot.com.dd\/files\/catalogues\/2016-09\/images\/Pty%20Prs.compressedjpg_Page1.jpg\">Property Press.compressedjpg_Page1.jpg<\/a>

i need to collect only image url instead of the whole link ,

sarath
  • 15
  • 5

1 Answers1

0

Well, sending HTML elements in a JSON doesn't seem good to but, anyway if you cannot change it...

For my part, I would parse the html string with the built-in XML parser.

Here is the code taken from this answer

//XML parser
var parseXml;

if (typeof window.DOMParser != "undefined") {
    parseXml = function(xmlStr) {
        //should work with any recent browser
        return ( new window.DOMParser()).parseFromString(xmlStr, "text/xml");
    };
} else if (typeof window.ActiveXObject != "undefined" &&
       new window.ActiveXObject("Microsoft.XMLDOM")) {
    //This part is intended to very old browsers
    parseXml = function(xmlStr) {
        var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(xmlStr);
        return xmlDoc;
    };
} else {
    throw new Error("No XML parser found");
}

//Your code

var jsonContent= [{"image":" <a href=\"http:\/\/docroot.com.dd:8083\/sites\/docroot.com.dd\/files\/catalogues\/2016-09\/images\/Pty%20Prs.compressedjpg_Page1.jpg\">Property Press.compressedjpg_Page1.jpg<\/a>"}];

var elem = jsonContent[0].image;

var link = parseXml(elem);

try {
 document.getElementById("out").innerHTML = link.documentElement.getAttribute("href");
} catch (e) {
  alert(e);
}
<span id="out" />
Community
  • 1
  • 1
C.Champagne
  • 5,381
  • 2
  • 23
  • 35