gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot read property 'length' of undefined

转载 作者:可可西里 更新时间:2023-11-01 01:31:29 24 4
gpt4 key购买 nike

当我尝试从 AJAX 调用中获取数据并在单击提交按钮时将其插入另一个函数时,如何避免出现以下错误?

ajax 函数中的 console.log 调用显示数据已被抓取,我希望然后存储在 json_data 中。

当时的目的是使用此数据来更改通过 HTML 表单提交的字符串。

然后在“点击”函数的行返回错误:

console.log(json_data.length);

<title>Test Form</title>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<style>
#results_box {
border: red 5px solid;
}

#place {
border: #cccccc 1px solid;

}
</style>
<script type="text/javascript">
$(document).ready(function() {
var json_source = "https://spreadsheets.google.com/feeds/list/0ApL1zT2P00q5dG1wOUMzSlNVV3VRV2pwQ2Fnbmt3M0E/od7/public/basic?alt=json";
var string_data ="";
var json_data = $.ajax({
dataType: 'jsonp',
url: json_source,
success: function(data){
var data_obj = [];
for (i=0; i<data.feed.entry.length; i++){
var el = {'key': data.feed.entry[i].title['$t'], 'value': '<p><a href="'+data.feed.entry[i].content['$t']+'>'+data.feed.entry[i].title['$t']+'</a></p>'};
data_obj.push(el)};

console.log("data grabbed");

return data_obj;

},

error: function(jqXHR, textStatus, errorThrown){
$('#results_box').html('<h2>Something went wrong!</h2><p><b>' + textStatus + '</b> ' + errorThrown + '</p>');
}
});

$(':submit').click(function(event, json_data){
event.preventDefault();
console.log(json_data.length);

//function
if ($('#place').val() !=''){
var copy_string = $('#place').val();
var converted_string = copy_string;
for (i=0; i<json_data.length; i++){
//console_log(data.feed.entry[i].title['$t']);
converted_string = converted_string.replace(json_data.feed.entry[i].title['$t'],
'<a href="'+json_data.feed.entry[i].content['$t']+'>'+json_data.feed.entry[i].title['$t']+'</a>');
}
$('#results_box').text(converted_string).html();
}
});

});//document ready end

</script>
</head>

<body>
<div id="wrapper">
<div id="query_box" class="panel">
<form id="form_submit"><h4>Copy to process:</h4>
<textarea id="place"></textarea>
<input type="submit" value="Go" />

</form>
</div>
<div id="results_box" >Results will appear here</div>
</div>

最佳答案

您没有正确传递变量。一种快速的解决方案是像这样创建一个全局变量:

var global_json_data;
$(document).ready(function() {
var json_source = "https://spreadsheets.google.com/feeds/list/0ApL1zT2P00q5dG1wOUMzSlNVV3VRV2pwQ2Fnbmt3M0E/od7/public/basic?alt=json";
var string_data ="";
var json_data = $.ajax({
dataType: 'json', // Return JSON
url: json_source,
success: function(data){
var data_obj = [];
for (i=0; i<data.feed.entry.length; i++){
var el = {'key': data.feed.entry[i].title['$t'], 'value': '<p><a href="'+data.feed.entry[i].content['$t']+'>'+data.feed.entry[i].title['$t']+'</a></p>'};
data_obj.push(el)};

console.log("data grabbed");
global_json_data = data_obj;

return data_obj;


},

error: function(jqXHR, textStatus, errorThrown){
$('#results_box').html('<h2>Something went wrong!</h2><p><b>' + textStatus + '</b> ' + errorThrown + '</p>');
}
});

$(':submit').click(function(event){
var json_data = global_json_data;
event.preventDefault();
console.log(json_data.length);

//function
if ($('#place').val() !=''){
var copy_string = $('#place').val();
var converted_string = copy_string;
for (i=0; i<json_data.length; i++){
//console_log(data.feed.entry[i].title['$t']);
converted_string = converted_string.replace(json_data.feed.entry[i].title['$t'],
'<a href="'+json_data.feed.entry[i].content['$t']+'>'+json_data.feed.entry[i].title['$t']+'</a>');
}
$('#results_box').text(converted_string).html();
}
});

});//document ready end

关于javascript - 未捕获的类型错误 : Cannot read property 'length' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19930214/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com