Javascript 更改HTTP web请求中的参数

Javascript 更改HTTP web请求中的参数,javascript,html,web-development-server,Javascript,Html,Web Development Server,嘿,伙计们,我对网络开发非常陌生。我使用FetchAPI发送http请求,如下所示。它工作得很好。但是现在我想在执行sql查询时更改第二个参数(即IP地址)。因此,每次我按下网页上的按钮时,都要执行sql查询到IP所在的位置。是否可以为sql查询设置一个变量,并将该变量写入IP?感谢您的帮助。谢谢 fetch(" http://192.xx.x.xxx/abc-bin/output?username=one&password=xxxx&action=on&

嘿,伙计们,我对网络开发非常陌生。我使用FetchAPI发送http请求,如下所示。它工作得很好。但是现在我想在执行sql查询时更改第二个参数(即IP地址)。因此,每次我按下网页上的按钮时,都要执行sql查询到IP所在的位置。是否可以为sql查询设置一个变量,并将该变量写入IP?感谢您的帮助。谢谢

 fetch("    http://192.xx.x.xxx/abc-bin/output?username=one&password=xxxx&action=on&pin=relay")
  .then(function (response) {
    return response.json();
  })

是的,这是javascript中非常常见的任务。你有很多选择来解决这个问题。如何设置变量将取决于前端javascript单击处理程序或按钮本身的html属性

选项1-字符串浓缩

// Create a variable with the IP or Domain you would like to replace with
// If you didn't know domains are mapped to IPs, so you can use each inter-changably


// Example variables
const protocol = 'https://'
const domain = 'www.stackoverflow.com'
const path = '/abc-bin/output?username=one&password=xxxx&action=on&pin=relay'

// Concatinate to create single string
const url = protocol + domain + path

fetch(url)
  .then(function (response) {
    return response.json();
  })
选项2-字符串插值

// Example variables
const domain = 'www.stackoverflow.com'

// Interpolate string...NOTE they aren't single quotes but 
// are backticks (top left key on US keyboard)
// You must wrap your variables with ${variableName} to
// have it interpreted as a string
const url = `https://${domain}/abc-bin/output?username=one&password=xxxx&action=on&pin=relay`

fetch(url)
  .then(function (response) {
    return response.json();
  })

是的。。。你可以用字符串做各种事情,fetch中的url只是一个字符串——当然,你要如何在浏览器中“执行sql”?执行sql查询,然后在处理结果的回调函数中,将你从DB中获取的IP连接到url中。重要的是要注意,你实际上并没有在这里修改sql。您正在修改一个HTTP请求,它看起来像是在调用RESTAPI。服务器接收HTTP请求,解释它们并执行一段代码,该代码将代表您与数据库进行交互,从而将SQL语言从交互中抽象出来