我有以下代码,它应该获取输入字段的值,将值发送到 imdbapi.org 并处理发回的 JSON。 我包含了一个显示输入字段值的元素,但对于一些字符串,它会截断字符串。参见 this test case
$("form > input#movname").keyup(function() {
var inputval = encodeURI($("form > input").val());
$.getJSON('http://imdbapi.org/',
{
title: inputval,
plot: "none",
limit: "5"
},
function(data) {
var items = [];
$.each(data, function(key, val) {
items.push('<li id="'+val.imdb_id+'">');
items.push('<img src="'+encodeURI(val.poster)+'" />');
items.push('<strong class="title">'+val.title+'</strong><br />');
items.push('<div>'+val.rated.replace("_"," ")+'</div>');
items.push('</li>');
});
items.push('<li id="debug">');
items.push('<img src="src/nocover.png" />');
items.push('<strong class="title">'+$("form > input").val()+'</strong><br />');
items.push('</li>');
var html = items.join('');
$("#suggestions").html("");
$('<ol/>', {
'class': 'moviessuggestions',
html: html
}).appendTo('#suggestions');
});
});
最佳答案
看起来 limit
是不必要的(用法错误?),使用
{
title: inputval,
plot: "none"
}
而且效果很好。
关于json - 处理输入值时出错,通过 JSON 发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13816208/