5

REST API を使用するプロジェクトがあります。ここでログイン要求を送信すると、データを含む JSON として応答が送信されます。応答ヘッダーのそれと一緒に

Access-Control-Allow-Origin:*
Connection:keep-alive
Content-Length:239
Content-Type:application/json
Date:Fri, 19 Oct 2012 06:28:12 GMT
Server:Apache/2.2.22 (Amazon)
Set-Cookie:session=username; Path=/

ここには がありますSet-Cookieが、この Cookie は設定されていません。他の API アクセスでは、サーバーがこの Cookie をチェックするため、この Cookie を設定する必要があります。

この問題を解決するにはどうすればよいですか? jQuery AJAX 応答ヘッダー Set-Cookie メソッドの解決策は何ですか?

4

2 に答える 2

6

XMLHTTPRequest からヘッダーを取得できます。これが役立つ場合があります。これが機能しているかどうか教えてください。

$.ajax({
   type: 'POST',
   url:'url.do',
   data: formData,
   success: function(data, textStatus, XMLHttpRequest){
        var cookietoSet=XMLHttpRequest.getResponseHeader('Set-Cookie');

        Set_Cookie(cookietoSet.split('=')[0],cookietoSet.split('=')[1],expires, path, domain, secure)//change as per ur needs
   }
   error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert(XMLHttpRequest.getResponseHeader('some_header'));
   }
  });




function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}
于 2012-10-19T06:55:25.123 に答える
2

ここでの本当の問題は、ブラウザーのプライバシー/Cookie 設定がパラノイドに設定されている場合、一部のブラウザー (少なくとも FF と IE) が XHR ベースの Set-Cookie ヘッダーを無視するように見えることだと思います。

ユーザーに役立つ FF ヘルプへのリンクを次に示します。 http://support.mozilla.org/en-US/kb/fix-login-issues-on-websites-require-passwords

この 8(

于 2013-02-18T08:58:50.713 に答える