从imdbapi
导入IMDB编号时,自动填写表格,我有以下形式: 从imdbapi
我想,当你进入一个imdb_id我会到IMDB API的调用,并获得其余的信息由id自动填写表单,如果id不匹配,用户将自行填写表单。
回答:
我使用OMDb API创建了一个非常简单的实现,我猜你正在使用的是API。
由于我没有API成员资格,所以我使用模拟数据创建了这个演示,但是如果您将API密钥放入代码并将模拟数据解析替换为响应,这应该可以正常工作。
我的JS:
var mockAladinData = '{"Title":"Aladin","Year":"2009","Rated":"N/A","Released":"30 Oct 2009","Runtime":"132 min","Genre":"Action, Adventure, Comedy","Director":"Sujoy Ghosh","Writer":"Vishal Dadlani (lyrics), Sujoy Ghosh (screenplay), Suresh Nair (additional story), Ritesh Shah (dialogue), Ritesh Shah (story)","Actors":"Amitabh Bachchan, Sanjay Dutt, Riteish Deshmukh, Jacqueline Fernandez","Plot":"Since he was a child, Aladin Chatterjee has been teased for his fairytale name. As a college student he follows his namesake\'s footsteps; unleashing genie Genius and wooing exchange student Jasmine. But the evil Ringmaster approaches.","Language":"Hindi","Country":"India","Awards":"3 wins.","Poster":"https://images-na.ssl-images-amazon.com/images/M/[email protected]@._V1_SX300.jpg","Ratings":[{"Source":"Internet Movie Database","Value":"4.6/10"}],"Metascore":"N/A","imdbRating":"4.6","imdbVotes":"2,075","imdbID":"tt1227762","Type":"movie","DVD":"08 Mar 2011","BoxOffice":"N/A","Production":"Eros International","Website":"http://www.aladin.erosentertainment.com/","Response":"True"}'; $("button").on("click", function(event) {
event.preventDefault();
var value = $(".movie-id")[0].value.trim(),
keys = ["Year", "Title"];
if (value) {
var url = "https://www.omdbapi.com/?i=" + value + "&apikey={your api key}";
$.ajax({
url: url,
error: function() {
$(".error-msg").show();
},
success: function(response) {
response = JSON.parse(mockAladinData);
if (response.Response != "False") {
$(".error-msg").hide();
for (var i = 0; i < keys.length; i++) {
$(".movie-" + keys[i].toLowerCase())[0].value = response[keys[i]];
}
} else {
$(".result-msg").show();
}
},
type: 'GET'
});
}
});
我的HTML:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form class="movie-form">
<div>IMDb movie id (example: tt1227762)</div>
<input class="movie-id" type="text"><button>Enter</button>
<div class="error-msg msg">Error</div>
<div class="result-msg msg">No results</div>
<div>Movie Title</div>
<input class="movie-title input" type="text">
<div>Movie year</div>
<input class="movie-year input" type="text">
</form>
<div>some more input fields</div>
我的CSS:
.input { display: block;
}
.msg {
display: none;
}
以上是 从imdbapi 的全部内容, 来源链接: utcz.com/qa/257577.html