【问题标题】:jQuery Cross Domain Request JSON: Cannot read property 'json' of nulljQuery 跨域请求 JSON:无法读取 null 的属性“json”
【发布时间】:2023-03-20 10:26:01
【问题描述】:

我正在尝试从外部 URL 获取 JSON。由于跨域问题,我使用 yahoo 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>

【问题讨论】:

    标签: jquery json yql


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢。现在,它没有出错,但我仍然没有得到对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 2016-06-23
    • 2013-08-19
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多