2017-08-08 73 views
5

Ben sonrası isteğiAksiylerde eksenler ve seçenekler nasıl ayarlanır?

import axios from 'axios' 
    params = {'HTTP_CONTENT_LANGUAGE': self.language} 
    headers = {'header1': value} 
    axios.post(url, params, headers) 

o coorect emin olmak için AXIOS kullanabilir?

veya axios.post(url, params: params, headers: headers) doğru mu?

cevap

4

Sen gibi AXIOS için bir yapılandırma nesnesi iletebilirsiniz:

Bunu yapmanın birkaç yolu vardır
axios({ 
    method: 'post', 
    url: '....', 
    params: {'HTTP_CONTENT_LANGUAGE': self.language}, 
    headers: {'header1': value} 
}) 
8

: tek bir istek için

  • :

    let config = { 
        headers: { 
        header1: value, 
        } 
    } 
    
    let data = { 
        'HTTP_CONTENT_LANGUAGE': self.language 
    } 
    
    axios.post(URL, data, config).then(...) 
    
  • için varsayılan genel yapılandırmayı ayarlama:

    let instance = axios.create({ 
        headers: { 
        post: {  // can be common or any other method 
         header1: 'value1' 
        } 
        } 
    }) 
    
    //- or after instance has been created 
    instance.defaults.headers.post['header1'] = 'value' 
    
1

Sen (örneğin JWT'de ile kimlik doğrulama için) Başlıklarla bir get isteği gönderebilirsiniz:

axios.get('https://example.com/getSomething', { 
headers: { 
    Authorization: 'Bearer ' + token //the token is a variable which holds the token 
} 
}) 
AXIOS örneğinde varsayılan olarak ayarlamak için
axios.defaults.headers.post['header1'] = 'value' // for POST requests 
axios.defaults.headers.common['header1'] = 'value' // for all requests 
  • Ayrıca posta talebinde bulunabilirsiniz.

    axios.post('https://example.com/postSomething', { 
    email: varEmail, //varEmail is a variable which holds the email 
    password: varPassword 
    }) 
    
    yapıyor yolum böyle bir istek ayarlamaktır:

    axios({ 
        method: 'post', //you can set what request you want to be 
        url: 'https://example.com/request', 
        data: {id: varID}, 
        headers: { 
        Authorization: 'Bearer ' + varToken 
        } 
    }) 
    
  • +0

    İkinci gönderi isteğiniz belirli başlıklar sağlamaz, tam örnek için düzenleyebilir misiniz? – Striped

    +0

    Sadece bir örnek nasıl gönderilirdi.Önemli olarak başlıklarla olmak zorunda değilsiniz.Bir başlık göndermek istiyorsanız, 3. örneği görebilirsiniz –

    1

    Bu örnek basit ve başlıklar + yapılandırma ile düzgün çalışır.

    var config = { 
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, 
        responseType: 'blob' 
    }; 
    
    axios.post('http://YOUR_URL', this.data, config) 
        .then((response) => { 
        console.log(response.data); 
    });