gpt4 book ai didi

javascript - 集成 VIN 解码器 API

转载 作者:行者123 更新时间:2023-12-01 01:48:59 26 4
gpt4 key购买 nike

我以前从未将 API 集成到我的任何应用程序中。我想集成一个基本的 VIN 解码器 API,它将在提交车辆的 VIN 后提供基本的车辆信息。我想使用 NHTSA 车辆 API,因为它免费且简单。我最熟悉 Javascript,他们提供了一个代码示例,我将在下面提供。如何将此 API 集成到我的简单 HTML 表单中?感谢您的帮助!

VIN 解码器链接:NHTSA VIN Decoder API

HTML:

<!DOCTYPE html>
<html>

<head>
<title>VIN Decoder API Test</title>

<style type="text/css">
input {width: 200px;}
</style>

<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

<script type="text/javascript">
</script>

</head>

<body>
<table align="center">
<tr>
<td align="center">
<input type="text" id="b12" placeholder="VIN - 17 Digits" name="name" maxlength="100">
</td>
</tr>
<tr>
<td align="center">
<button>Submit</button>
</td>
</tr>
<tr>
<td>
<textarea rows="15" cols="100" placeholder="Vehicle Data Presented Here"></textarea>
</td>
</tr>
</table>


</body>
</html>

“Javascript 与 jQuery,获取示例”:

$.ajax({
url: "https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMakeId/440?format=json",
type: "GET",
dataType: "json",
success: function(result)
{
console.log(result);
},
error: function(xhr, ajaxOptions, thrownError)
{
console.log(xhr.status);
console.log(thrownError);
}
});

“Javascript 与 jQuery,发布示例”:

$.ajax({
url: "https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVINValuesBatch/",
type: "POST",
data: { format: "json", data: "3GNDA13D76S000000;5XYKT3A12CG000000;"},
dataType: "json",
success: function(result)
{
console.log(result);
},
error: function(xhr, ajaxOptions, thrownError)
{
console.log(xhr.status);
console.log(thrownError);
}
});

最佳答案

HTML:

<table align="center">
<tr>
<td align="center">
<input type="text" id="b12" placeholder="Enter VINs-separated by ;" name="b12" maxlength="100"/>
</td>
</tr>
<tr>
<td align="center">
<button id="submit_btn">Submit</button>
</td>
</tr>
<tr>
<td>
<textarea rows="15" cols="100" id="results" placeholder="Vehicle Data Presented Here"></textarea>
</td>
</tr>
</table>

Javascript:

$("#submit_btn").click(function () {
var val = $("#b12").val();

$.ajax({
url: "https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVINValuesBatch/",
type: "POST",
data: { format: "json", data: val},
dataType: "json",
success: function(result)
{
$("#results").val(JSON.stringify(result));
},
error: function(xhr, ajaxOptions, thrownError)
{
console.log(xhr.status);
console.log(thrownError);
}
});
})

我将让您将结果解析为您想要呈现的内容...

关于javascript - 集成 VIN 解码器 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51734389/

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