GET请求是HTTP协议中最常用的方法之一,用于从服务器获取资源。URL参数(也称为查询参数)是附加在URL末尾的键值对,用于向服务器传递额外的信息。
URL参数的格式是在URL后加问号(?)开始,多个参数用&连接:
https://api.example.com/resource?param1=value1¶m2=value2
const baseUrl = 'https://api.example.com/data';
const params = {
userId: 12345,
category: 'books',
limit: 10
};
// 构建查询字符串
const queryString = Object.keys(params)
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
.join('&');
const url = `${baseUrl}?${queryString}`;
fetch(url)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
import axios from 'axios';
axios.get('https://api.example.com/data', {
params: {
userId: 12345,
category: 'books',
limit: 10
}
})
.then(response => console.log(response.data))
.catch(error => console.error('Error:', error));
const express = require('express');
const app = express();
app.get('/api/data', (req, res) => {
const { userId, category, limit } = req.query;
// 使用参数处理逻辑...
res.json({ userId, category, limit });
});
app.listen(3000, () => console.log('Server running on port 3000'));
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/api/data', methods=['GET'])
def get_data():
user_id = request.args.get('userId')
category = request.args.get('category')
limit = request.args.get('limit')
# 使用参数处理逻辑...
return jsonify({'userId': user_id, 'category': category, 'limit': limit})
if __name__ == '__main__':
app.run(debug=True)
原因:未对参数进行正确的URL编码
解决:使用encodeURIComponent()
或相应语言的URL编码函数
原因:
原因:GET请求的URL长度超过服务器或浏览器限制 解决:
没有搜到相关的文章