简体   繁体   English

通过输入字段数据调用外部api

[英]Call external api via input field data

Assume a 'part number' input field that is not in a form tag.假设“部件号”输入字段不在表单标签中。 When the user enters a value, the onblur or a button next to the input field should query an external site via that site's API, and return the 'product name' value.当用户输入一个值时,输入字段旁边的 onblur 或按钮应通过该站点的 API 查询外部站点,并返回“产品名称”值。

I have the code that will use the API to return the value.我有将使用 API 来返回值的代码。 But how to structure the input field so that it calls the API code?但是如何构造输入字段以使其调用 API 代码?

I think I need an AJAX call, but still a noob on Ajax stuff.我想我需要一个 AJAX 电话,但仍然是 Ajax 的菜鸟。 What code would be used to process the string in the input field.将使用什么代码来处理输入字段中的字符串。 (Note that there is no form tag associated with the input field.) (I primarily use PHP.) (请注意,没有与输入字段关联的表单标签。)(我主要使用 PHP。)

In response to the question and subsequent comments, conceptually it would work as follows then.针对问题和随后的评论,从概念上讲,它将按如下方式工作。

The HTML Markup: HTML 标记:

<input type="text" id="userInput"> <!--Text input of product id from user-->
<button id="btn">Get Product Name</button> <!--Button to invoke API request-->
<p id="productName"></p> <!--Element used to display product name result from API-->

The Javascript: Javascript:

//Attach an event listener to the button. 
document.getElementById("btn").addEventListener("click", function(){

    //Get the id of product the user entered
    var productId = document.getElementById("userInput").value;

    //pass productId to the Amazon API

    //receive product name from API

    //Display product name received
    document.getElementById("productName").innerHTML = //The product name received from API

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 流星:来自外部API调用的数据未呈现 - Meteor: Data from External API call not rendering 通过输入字段更新javascript数组数据 - Updating javascript array data via input field 如何通过 html 页面上的 ajax 调用加载外部 JSON 数据? - How to load external JSON data via an ajax call on html page? HTTP API调用拒绝接受数据字段 - HTTP API call refuses to accept a data field 使用来自外部 Javascript Websocket 的数据更新 HTML 输入字段 - update a HTML input field with the data coming from a external Javascript Websocket 使用来自外部 url 的 JSON 数据的自动完成字段(Steam API) - Autocomplete field with JSON data from external url (Steam API) 调用外部API并使用Node.JS显示JSON数据 - Call external API and display JSON data using Node.JS 通过Google Analytics(分析)API显示单个数据字段 - Showing single data field via Google Analytics API 通过API调用React Flux加载初始数据 - React Flux Loading Initial Data via API Call 如何通过 API “input type=&#39;file&#39;” 标签使用本地文件更改视频源...没有 Jquery 或外部库 - How to change video source with local file via API "input type='file' " tag...NO Jquery or external libraries
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM