簡體   English   中英

更改 HTTP Web 請求中的參數

[英]Change parameters in HTTP web requests

嘿伙計們,我對 Web 開發很陌生。 我使用 fetch API 發送一個 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();
  })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM