Home How to serve a json that was fetched via a POST call?
Reply: 1

How to serve a json that was fetched via a POST call?

kane Published in 2018-01-12 16:19:32Z

I have a button that invokes a POST call which retrieves a json response from the server. I'd like to open a new (chrome) browser tab with this json response.

This is what I have so far in angular

  .then(function(res) {
    $scope.toJSON = angular.toJson(res.data);
    var blob = new Blob([$scope.toJSON], { type:"application/json;charset=utf-8;" });

    var downloadLink = angular.element('<a></a>');

This code will open a new tab with the json in it, but it doesn't seem to be recognized as json. What I mean by this is that I have a chrome extension (JSONView), which will automatically pretty print any json. But it doesn't seem to pretty print this one.

If it matters, the new tab has an url that looks like this


I've tried a bunch of different chrome extensions for viewing json and they all exhibit the same effect. It's possible the extensions can't process this "blob:chrome-extension://" page.

So what's the right way to open a json in a new tab?

Clarification I don't want to display this on the page. I want to open it in a new tab. I should mention that this is code within a Chrome Extension, not a standard web page

guest271314 Reply to 2018-01-12 16:39:12Z

Instead of using an extension you can use <pre> element and JSON.stringify()

.then(response => response.json())
.then(json => {
  document.querySelector("pre").textContent = JSON.stringify(json, null, 2)

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.428625 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO