4ndy 4ndy - 3 years ago 212
Ajax Question

Ajax Get is logged in console but won't print to page

Can someone point out the obvious as to why this response isn't printing to the page?

$.ajaxPrefilter( function (options) {
if (options.crossDomain && jQuery.support.cors) {
var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
contentType: 'application/json; charset=utf-8',
options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
//options.url = "http://cors.corsproxy.io/url=" + options.url;

function (data) {
console.log("THE RESPONSE ", data);
}, 4000);



<div id="viewer"></div>


JSFiddle here shows that a JSON object is logged in the console.
I want to access the data in the response. I thought I'd start with just printing out the whole response to the "viewer" div but nothing appears there.

Answer Source

The reason nothing is rendered is because $.html() accepts a string to render as HTML, and you are providing an object.

If you want your object to be printed as a string, you can refactor to:


I think this will be really noisy, especially if the object is large. For debugging purposes, I think you are better off using the console.

Fiddle: https://jsfiddle.net/0x3j7fk1/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download