2017-09-25 65 views
0

我已使用axios呼叫網絡請求react-Native但它返回500響應代碼Axios post請求頭不工作

注意:它在郵遞員客戶端工作完美。

授權令牌:

承載eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6Ly9hcHBzZGF0YTIuY2xvdWRhcHAubmV0L2p3dC1kZW1vL3B1YmxpYy9hcGkvbG9naW4iLCJpYXQiOjE1MDYzMzkwNDgsImV4cCI6MTY2MTg1OTA0OCwibmJmIjoxNTA2MzM5MDQ4LCJqdGkiOiJHUWt5RVlwck5GSDBHekd4In0.JqdyAEkEN_D3M2WbqcQwIwghk6iajFjxi9g854akjB8

代碼

從嘗試捕捉
const token = 'Bearer '.concat(this.state.token); 

var headers = { 
    'Content-Type': 'application/json', 
    'Authorization': token 
} 

axios.post('http://appsdata2.cloudapp.net/jwt-demo/public/api/profile', 
    null, 
    headers) 
.then((response) => { 
    console.log("reactNativeDemo","response get details:"+response.data); 
}) 
.catch((error) => { 
    console.log("axios error:",error); 
}); 

登錄

'axios error:', { [Error: Request failed with status code 500] 
config: 
{ adapter: [Function: xhrAdapter], 
    transformRequest: { '0': [Function: transformRequest] }, 
    transformResponse: { '0': [Function: transformResponse] }, 
    timeout: 0, 
    xsrfCookieName: 'XSRF-TOKEN', 
    xsrfHeaderName: 'X-XSRF-TOKEN', 
    maxContentLength: -1, 
    validateStatus: [Function: validateStatus], 
    headers: 
    { Accept: 'application/json, text/plain, */*', 
    'Content-Type': 'application/x-www-form-urlencoded' }, 
    method: 'post', 
    'Content-Type': 'application/json', 
    Authorization: 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6Ly9hcHBzZGF0YTIuY2xvdWRhcHAubmV0L2p3dC1kZW1vL3B1YmxpYy9hcGkvbG9naW4iLCJpYXQiOjE1MDYzMzkwNDgsImV4cCI6MTY2MTg1OTA0OCwibmJmIjoxNTA2MzM5MDQ4LCJqdGkiOiJHUWt5RVlwck5GSDBHekd4In0.JqdyAEkEN_D3M2WbqcQwIwghk6iajFjxi9g854akjB8', 
    url: 'http://appsdata2.cloudapp.net/jwt-demo/public/api/profile', 
    data: null }, 
    request: 
    { UNSENT: 0, 
    OPENED: 1, 
    HEADERS_RECEIVED: 2, 
    LOADING: 3, 
    DONE: 4, 
    readyState: 4, 
    status: 500, 
    timeout: 0, 
    withCredentials: true, 
    upload: {}, 
    _aborted: false, 
    _hasError: false, 
    _method: 'POST', 
    _response: '{"meta":{"message":"Unable to get token in your request."}}', 
    _url: 'http://appsdata2.cloudapp.net/jwt-demo/public/api/profile', 
    _timedOut: false, 
    _trackingName: 'unknown', 
    _incrementalEvents: false, 
    responseHeaders: 
    { expires: '-1', 
    pragma: 'no-cache', 
    Server: 'nginx', 
    Date: 'Mon, 25 Sep 2017 11:30:49 GMT', 
    'Keep-Alive': 'timeout=60', 
    'Content-Type': 'application/json', 
    'Content-Length': '59', 
    Connection: 'keep-alive', 
    'X-RateLimit-Remaining': '58', 
    'X-Powered-By': 'PHP/5.6.23-1+deprecated+dontuse+deb.sury.org~trusty+1', 
    'Cache-Control': 'private, must-revalidate', 
    'X-RateLimit-Limit': '60' }, 
    _requestId: null, 
    _cachedResponse: undefined, 
    _headers: 
    { accept: 'application/json, text/plain, */*', 
    'content-type': 'application/x-www-form-urlencoded' }, 
    _responseType: '', 
    _sent: true, 
    _lowerCaseResponseHeaders: 
    { expires: '-1', 
    pragma: 'no-cache', 
    server: 'nginx', 
    date: 'Mon, 25 Sep 2017 11:30:49 GMT', 
    'keep-alive': 'timeout=60', 
    'content-type': 'application/json', 
    'content-length': '59', 
    connection: 'keep-alive', 
    'x-ratelimit-remaining': '58', 
    'x-powered-by': 'PHP/5.6.23-1+deprecated+dontuse+deb.sury.org~trusty+1', 
    'cache-control': 'private, must-revalidate', 
    'x-ratelimit-limit': '60' }, 
    _subscriptions: [], 
    responseURL: 'http://appsdata2.cloudapp.net/jwt-demo/public/api/profile' }, 
    response: 
    { data: { meta: { message: 'Unable to get token in your request.' } }, 
    status: 500, 
    statusText: undefined, 
    headers: 
    { expires: '-1', 
    pragma: 'no-cache', 
    server: 'nginx', 
    date: 'Mon, 25 Sep 2017 11:30:49 GMT', 
    'keep-alive': 'timeout=60', 
    'content-type': 'application/json', 
    'content-length': '59', 
    connection: 'keep-alive', 
    'x-ratelimit-remaining': '58', 
    'x-powered-by': 'PHP/5.6.23-1+deprecated+dontuse+deb.sury.org~trusty+1', 
    'cache-control': 'private, must-revalidate', 
    'x-ratelimit-limit': '60' }, 
    config: 
    { adapter: [Function: xhrAdapter], 
    transformRequest: { '0': [Function: transformRequest] }, 
    transformResponse: { '0': [Function: transformResponse] }, 
    timeout: 0, 
    xsrfCookieName: 'XSRF-TOKEN', 
    xsrfHeaderName: 'X-XSRF-TOKEN', 
    maxContentLength: -1, 
    validateStatus: [Function: validateStatus], 
    headers: 
    { Accept: 'application/json, text/plain, */*', 
    'Content-Type': 'application/x-www-form-urlencoded' }, 
    method: 'post', 
    'Content-Type': 'application/json', 

回答

2

愛可信崗位方法的語法如下: axios.post(url[, data[, config]])

所以,你應該做這樣的事情:

axios.post('http://appsdata2.cloudapp.net/jwt-demo/public/api/profile', 
null, 
{headers: headers}) 
.then((response) => { 
    console.log("reactNativeDemo","response get details:"+response.data); 
}) 
.catch((error) => { 
    console.log("axios error:",error); 
}); 

https://npm.runkit.com/axios嘗試這個並且效果不錯 Please check here

+1

謝謝Apurva,這個答案拯救了我:) – Annu

0

我有這樣的問題了。如果你想嘗試這個,如果這可以解決你的問題。

注意:這是配置您的global options。如果這對你的作品嚐試創建instances

const setAxiosToken = (token) => { 
    axios.defaults.headers.common['Authorization'] = `Bearer ${token}` 
    axios.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8' 
} 
setAxiosToken(YOUR_TOKEN) 
axios.post('http://appsdata2.cloudapp.net/jwt-demo/public/api/profile')