1

[Update] there is a 50 point bonus, which I will up to 200 for a working fiddle


[Update] while I prefer an AngualrJs solution, I will also accept plain JS - just anything to get me over this impasse ... a GET call to my server returns a URL and I want to embed that document into my HTML


With reference to my previous question, @MaximShoustin 's answer seemed perfect, but I am having problems.

The URL in that solution there is hard coded, but I want to get mine by AJAX. When I do, the document is not embedded, but I see no error in the developer console.

I made this fiddle, where I added these lines

to the HTML

  <iframe ng-src="{{cvUrlTrusted_2}}"></iframe>

and, to the controller

  app.controller('Ctrl', function($scope, $sanitize, $sce, $http) {

added , $http

and

  //  new stuff follows
  var url = 'http://fiddleapi.rf.gd/getDocuemntUrl.php';

  /* The URL contains this code ...
      <?php
        echo('https://d9db56472fd41226d193-1e5e0d4b7948acaf6080b0dce0b35ed5.ssl.cf1.rackcdn.com/spectools/docs/wd-spectools-word-sample-04.doc');   
                ?>     
  */
        $http.get(url)
      .success(function(data, status, headers, config)
         { 
              var cvTrustedUrl_2 = 'http://docs.google.com/gview?url=' + data.trim() + '&embedded=true';
              $scope.cvTrustedUrl = $sce.trustAsResourceUrl(cvTrustedUrl_2);
        })
         .error(function(data, status, headers, config)
          {
             alert('Urk!');
          });      

If you invoke the API at http://fiddleapi.rf.gd/getDocuemntUrl.php you will see that it returns the same document URL as was hard coded in the solution.

Please, first check my code, lest I have made a mistake.

Long description, short question : how can I embed a document who's URL is returned from an AJAX API into an HTML document using AngularJS? Free free to fork the fiddle.

4
  • look in console... there are errors displayed Commented Oct 29, 2017 at 13:41
  • Sorry, but I am not seeing them - maybe I use the wrong browser? Can you tell what they are and how to correct? Commented Oct 29, 2017 at 13:43
  • 1
    angular.js:12520 ReferenceError: $http is not defined Commented Oct 29, 2017 at 13:46
  • Aha! Thanks; ok, I will inject it & update the link in question. Meanwhile, can you answer the problem? Commented Oct 29, 2017 at 13:47

1 Answer 1

1
+50

Your fiddle doesn't work because of cross domain problem: http://fiddleapi.rf.gd/getDocuemntUrl.php

So I loaded simple JSON file with content:

{
  "val":"https://d9db56472fd41226d193-1e5e0d4b7948acaf6080b0dce0b35ed5.ssl.cf1.rackcdn.com/spectools/docs/wd-spectools-word-sample-04.doc"
}

and:

$http.get('data.json').then(function (resp){
      var cvTrustedUrl_2 = 'http://docs.google.com/gview?url=' + resp.data.val + '&embedded=true';
    $scope.cvUrlTrusted_2 = $sce.trustAsResourceUrl(cvTrustedUrl_2); 
});

Demo Plunker

It works fine so the problem is in your http://fiddleapi.rf.gd/getDocuemntUrl.php because this URL doesn't work in Postman too. I get:

This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support

be sure you configured it well

Sign up to request clarification or add additional context in comments.

6 Comments

That look great (+1), but I need to be able to get the URL from a 3rd party site :-( Would it help if I change the content of fiddleapi.rf.gd/getDocuemntUrl.php to have the same contents as your data,js ?
@Mawg The browser will block you with cross Domain security prob.
OIC :-( Then, if there is no other way, I will have to generate the whole HTML page server side in PHP, not load the HTML and then try to get that URL by AJAX. If I generate the whole page server side, then I can include the URL hard coded in it.
you can attach PHP file to your angular project and PHP will call server side PHP. So you will get rid of cross domain
A very good point, of course (as always, from you :-) I am coding something for a friend, and he wanted 2 sites, although I recommended only one. I will see if I can persuade him.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.