简体   繁体   中英

How can I send query parameters using fetch to server?

I have an express.js server that makes a HTTP GET Request to retrieve weather data. In my JavaScript, I obtain the latitude and longitude variables. I need to send those variables to my express.js server using fetch. How can I do this?

I have tried sending the query parameters as the body of the request object, but I learned GET requests cannot do that.

Express Server Code:

app.get("/data", (req, res) => {
url  =  `http://api.openweathermap.org/data/2.5/uvi?appid=${API}&lat=${latitude}&lon=${longitude}`;

axios
    .get(url)
    .then(response  => {
        res.send(response.data);
    })
    .catch(error  => {
        console.log(errorx);
    });
});

How would I get the latitude and longitude variables from my JavaScript?

I expect the express server to have the required variables and perform the GET request.

I'll share with you an example I've done before. works for me.

/server.js

var http = require('http');
var fs = require('fs');

var express = require('express');
var app = express();
var path = require('path');

app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
});

app.use('/app', express.static(__dirname + '/app'));
app.get('/', function (req, res) {
    res.sendFile(path.join(__dirname + '/app/index.html'));//my app page , may be different for you
});

var routes = require('./scripts/routes'); 
routes(app); 

app.listen(8080);

console.log('-> Port : 8080');

/scripts/routes.js

'use strict';


module.exports = function (app) {
    var todoList = require('../scripts/controller');

    app.route('/company/:companyId')
        .get(todoList.getInfo)

    app.route('/product-add/:name/:meter')
        .get(todoList.addPro);    

};

/scripts/controller.js

'use strict';

var sql = require("mssql");//I used mssql. may be different for you
var request = new sql.Request();

exports.getInfo = function (req, res) {


var comId = req.params.companyId

request.query('select * from company where id='+comId , function (err, recordset) {

    if (err) console.log(err)

    res.json(recordset);

});


};

exports.addPro = function (req, res) {

   // req.params.name 
  // req.params.meter

};

query address : http://127.0.0.1:8080/company/15

Query string parameters are part of the request URL.

Try the following to pass latitude and longitude when you do a GET request assuming the endpoint is http://localhost:3000/data?lat=0&long=0

app.get("/data", (req, res) => {
  // Add this to retrieve query string values
  let latitude = req.query.lat;
  let longitude = req.query.long;

  // Rest of your code
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM