2017-11-15 8 views
0

私はちょっと立ち往生しましたが、助言を求めています。Sencha ArchitectのDreamFactoryを使用してログインウィンドウ//認証を設定しようとしています。かなり初心者です。Sencha Architect 4 - ExtJS 6.5 - > Dreamfactory 2.9認証セッショントークン

私の現在のコードは次のとおりです。

var username = Ext.getCmp('usr').getValue(), 
password = Ext.getCmp('pwd').getValue(); 

Ext.Ajax.defaultHeaders = { 
'X-DreamFactory-Api-Key' : 'xyz', 
'Content-Type' : 'application/json' 
}; 

Ext.Ajax.request({ 
url: 'http://dreamfactoryaddy:8080/api/v2/user/session', 
withCredentials: true, 
email: username, 
password: password, 
cors: true, 

success: function(response) { 
    var obj = Ext.decode(response.responseText); 
    Ext.Ajax.defaultHeaders = { 
     'X-DreamFactory-Api-Key' : 
'xyz', 'Content-Type' : 'application/json', 'X-DreamFactory-Session- 
Token': obj.session_token 
    }; 
    console.log(obj); 
}, 

failure: function(response) { 
     console.log(response.responseText); 
} 

}); 

私はこのエラーが表示されます。

{"error":{"code":401,"context":null,"message":"There is no valid session for the current request.","status_code":401}} 

http://prntscr.com/hate2e

あなたが私に適切にセッションを処理するために、どのようにいくつかのアドバイスを与えることはできますか?

UPDATE 2:

  var username = Ext.getCmp('usr').getValue(), 
       password = Ext.getCmp('pwd').getValue(), 
       headers = { 
        'X-DreamFactory-Api-Key' : 'xyz', 
        'Content-Type' : 'application/json' 
       }; 

      Ext.Ajax.setDefaultHeaders(headers); 
      Ext.Ajax.request({ 
       url: 'http://chalton.swiftmedia.ca:8080/api/v2/user/session', 
       withCredentials: true, 
       jsonData: { 
        "email" : username, 
        "password" : password 
       }, 
       cors: true, 

       success: function(response) { 
        var obj = Ext.util.JSON.decode(response.responseText), 
         newheaders = { 
          'X-DreamFactory-Api-Key' : 'xyz', 
          'Content-Type' : 'application/json', 
          'X-DreamFactory-Session-Token': obj.session_token 
         }; 
        console.log(response.responseText); 
        Ext.Ajax.setDefaultHeaders(newheaders); 
       }, 

       failure: function(response) { 
        console.log(response.responseText); 
       } 

      }); 
     }, 
+0

私はDreamFactoryのローカルホスト用CORSを有効にしていることを忘れていました - > http://prntscr.com/hatj1v –

答えて

0

ブラウザのコンソールをチェックすると、あなたはdefaultHeadersが正しくAJAXリクエストに適用されないことがあります。

私は、デフォルトのヘッダーとして必要な任意の他のヘッダーと同じ問題を抱えていました。 Ext.Ajax.defaultHeaders = xちょうど動作しません、Ext.Ajax.setDefaultHeaders(x)します。

+0

ちょっとアレキサンダー、ちょうど次のコードで試してみました。行方不明?私は元のコードに更新を加えました。 –

+0

最初のヘッダーは渡されますが、session_tokenの2番目のヘッダーは渡されません。 http://prntscr.com/hava87 –

+0

上記の問題を発見し、上記のコードを更新しました。リクエストがJSONとして渡されていませんでした。アレクサンダーありがとう! –

関連する問題