Home Web Front-end uni-app How to set request header in uniapp

How to set request header in uniapp

Apr 20, 2023 pm 03:01 PM

When developing using uniapp recently, sometimes it is necessary to set request header information in the request so that the server can process the request correctly. Let’s share how to set request headers in uniapp.

  1. Set the request header in main.js

You can set the request header information globally in the project's main.js, so that in any request will carry this information. The specific method is as follows:

import Vue from 'vue'
import App from './App'

Vue.config.productionTip = false

Vue.prototype.$http = function(url, method, data){
  return new Promise((resolve, reject) => {
    uni.request({
      url: 'http://localhost:8080/' + url,
      method: method,
      data: data,
      header:{
          'Content-Type': 'application/json',
          'Authorization': 'Bearer ' + uni.getStorageSync('token')
      },
      success: (res) => {
        resolve(res);
      },
      fail: (res) => {
        reject(res);
      }
    });
  });
}

App.mpType = 'app'

const app = new Vue({
  ...App
})
app.$mount()
Copy after login

In the above code, header is the request header information that needs to be set, where Content-Type represents the type of request data, Authorization represents the user's access token, which can be modified according to the actual situation.

  1. Set request headers in a single request

Sometimes, we may need to set a certain request header information in a single request. At this time, we can set the header in the uni.request method. The sample code is as follows:

uni.request({
  url: 'http://localhost:8080/' + url,
  method: method,
  data: data,
  header:{
    'Authorization': 'Bearer ' + uni.getStorageSync('token')
  },
  success: (res) => {
    resolve(res);
  },
  fail: (res) => {
    reject(res);
  }
});
Copy after login
  1. Notes

When using request headers, you need to pay attention to the following points:

  • Different backend frameworks may process request headers differently, and the request header information needs to be adjusted according to the actual situation.
  • The access token in the request header must be valid, otherwise the server may reject the request.
  • The data type in the request header needs to match the actual type of the request data in order to be processed correctly by the server.
  1. Summary

Through the above introduction, I believe everyone already understands the method of setting request headers in uniapp. In actual development, setting according to your actual needs can improve the interactive experience and data security.

The above is the detailed content of How to set request header in uniapp. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1255
24