2017-09-25 10 views
0

私はをreact-Nativeで呼び出していますが、500応答コードを返します。Axios投稿のヘッダが機能していない

注::郵便はがきのクライアントでは問題なく動作します。

許可トークン:

ベアラ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メソッドの構文を作成しようとするため、これが動作する場合は、次のとおりです。 axios.post(url[, data[, config]])

ですから、このような何か行う必要があります。私はhttps://npm.runkit.com/axiosでこれを試してみました

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); 
}); 

をとよく動作します 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') 
関連する問題