jQuery跨域请求JSON:无法读取null的属性'json'

时间:2016-06-30 06:26:21

标签: jquery json yql

我试图从外部网址获取JSON。由于跨域问题,我使用雅虎YQL服务。

我收到错误:无法读取null的属性'json'

感谢您的提示!

$.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>

1 个答案:

答案 0 :(得分:1)

您需要在json响应中添加null检查,请参阅更新后的代码

&#13;
&#13;
$.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"
    },
    
           
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

相关问题