mm1975 mm1975 - 5 months ago 23
JSON Question

jQuery Cross Domain Request JSON: Cannot read property 'json' of null

I´m trying to get a JSON from an external url. Due to Cross Domain-issues, I use the yahoo YQL service.

I get an error: Cannot read property 'json' of null

Thank you for your tips!



$.ajax({
url: "http://query.yahooapis.com/v1/public/yql",
dataType: "jsonp",
jsonp: "callback",
error: function() {
alert('There is an error with rawdata');
},
success: function(response) {

schema = [];
object = [];
data = [];

var schema = response.query.results.json.schema;
var options = response.query.results.json.options;
var data = response.query.results.json.data;

console.log( "schema: ", schema ); // server response
console.log( "options: ", options ); // server response
console.log( "data: ", data ); // server response

//$("#productEditor").alpaca("destroy");
//jsonEditor(schema, options, data);


}, data: {
q: "select * from json where url=\"https://www.webongo.de/data.json?format=json?callback=?\"",
format: "json"
},


});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>




Answer

You need to add a null check on the json response, see the updated code

$.ajax({
    url: "http://query.yahooapis.com/v1/public/yql",
    dataType: "jsonp",
    jsonp: "callback",
    error: function() {
    	alert('There is an error with rawdata');
    },
    success: function(response) {

		schema = [];
		object = [];
		data = [];
		if(response.query.results!==null){
          var schema = response.query.results.json.schema;
          var options = response.query.results.json.options;
          var data = response.query.results.json.data;

          console.log( "schema: ",  schema ); // server response
          console.log( "options: ",  options ); // server response
          console.log( "data: ",  data ); // server response

        	//$("#productEditor").alpaca("destroy");
    		//jsonEditor(schema, options, data);
       }else{
            console.log("Results returns null");
       }
    }, data: {
        q: "select * from json where url=\"https://www.webongo.de/data.json?format=json?callback=?\"",
        format: "json"
    },
    
           
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments