[英]Change portion of URL to show different data in JavaScript
In this code, I parse data from a JSON url into a table.在此代码中,我将 JSON url 中的数据解析到表中。
When a certain portion of the URL becomes altered, different search results come out.当 URL 的某个部分被更改时,会出现不同的搜索结果。 So I'm trying to give the user an option to input a different keyword that will bring different results.所以我试图给用户一个选项来输入一个不同的关键字,这将带来不同的结果。
In python I would use af string request, is there a similar option in JavaScript?在 python 中,我会使用 af 字符串请求,JavaScript 中是否有类似的选项?
Python example:蟒蛇示例:
device_name = simpledialog.askstring(title="510K Database",
prompt="Enter Keyword:")
r = requests.get(f'https://api.fda.gov/device/510k.json?search=device_name:{device_name}&limit=1000')`
My JavaScript code我的 JavaScript 代码
$(document).ready( function () {
var table = $('#example').DataTable({
ajax: {
url: 'https://api.fda.gov/device/510k.json?search=device_name:glucose&limit=1000',
dataSrc: 'results',
cache: true,
},
many thanks非常感谢
You would prompt and then use that variable as a template literal in your URL您会提示,然后将该变量用作 URL 中的模板文字
$(document).ready(function() { $('#echo').click(function() { $('.error').empty(); let keyword = $('#keyword').val().trim(); if (!keyword) return $(this).prev('.error').html("Please enter a keyword first"); console.log(`The keyword is ${keyword}`); /* let table = $('#example').DataTable({ ajax: { url: `https://api.fda.gov/device/510k.json?search=device_name:${keyword}&limit=1000`, dataSrc: 'results', cache: true, }, // .... }) */ }) })
.error { color: #f00; padding: 5px 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <label>510k Database // Enter Keyword <input id="keyword" /> </label> <div class='error'></div> <button id='echo'>Click me</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.