Advertisement
Guest User

Untitled

a guest
Oct 30th, 2017
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //i wrote a simple django rest framework app that requires a request like this
  2. ```python
  3. import requests
  4.  
  5. token = 'dfacbbd0718c4f152e3914ff70be81f8c9a4c8f5'
  6. headers = {
  7.   'Authorization': 'Token ' + token
  8. }
  9. r = requests.get('http://localhost:8000/backend/api/declarations/',
  10.                  headers=headers)
  11. print(r._content)
  12. ```
  13. //then i use angular4 to login and it is succesfull (with CORSE plugin for firefox)
  14.  
  15. login(username: string, password: string): Observable<boolean> {
  16.     let headers = new Headers({ 'Content-Type': 'application/json'});
  17.     let options = new RequestOptions({ headers: headers });
  18.     return this.http.post('http://localhost:8000/backend/users/login/', JSON.stringify({username: username, password: password}), options)
  19.       .map((response: Response) => {
  20.         console.log(response.json());
  21.         // login successful if there's a jwt token in the response
  22.         let token = response.json() && response.json().token;
  23.         let session_cookie_name = response.json() && response.json().session_cookie_name;
  24.         let session_cookie_key = response.json() && response.json().session_cookie_key;
  25.         let session_cookie_expire = response.json() && response.json().session_cookie_expire;
  26.         let session_cookie_path = response.json() && response.json().session_cookie_path;
  27.         let session_cookie_domain = response.json() && response.json().session_cookie_domain;
  28.         if (token) {
  29.           // set token property
  30.           this.token = token;
  31.  
  32.  
  33.           // store username and jwt token in local storage to keep user logged in between page refreshes
  34.           localStorage.setItem('currentUser', JSON.stringify({ username: username, token: token }));
  35.  
  36.           // return true to indicate successful login
  37.  
  38.           return true;
  39.         } else {
  40.           // return false to indicate failed login
  41.           return false;
  42.         }
  43.       });
  44.   }
  45.  
  46.  
  47. //problem starts when i want to get data using token
  48.  
  49.  
  50. import { Component, OnInit } from '@angular/core';
  51. import {DeclarationService} from '../declaration/declaration.service';
  52. import {Declaration} from '../declaration/declaration/declaration';
  53. import {Http, Headers, RequestOptions} from '@angular/http';
  54.  
  55. @Component({
  56.   selector: 'app-declaration-list',
  57.   templateUrl: './declaration-list.component.html',
  58.   styleUrls: ['./declaration-list.component.css']
  59. })
  60. export class DeclarationListComponent implements OnInit {
  61.   declarations: Declaration[] = [];
  62.   declarationsNegative: Declaration[] = [];
  63.   declarationsPositive: Declaration[] = [];
  64.   // headers: Headers;
  65.   constructor(public declarationService: DeclarationService, public http: Http) { }
  66.  
  67.   getAuthorizationHeader(): Headers {
  68.  
  69.     let headers = new Headers({ 'Content-Type': 'application/json' });
  70.     console.log(JSON.parse(localStorage.getItem('currentUser')).token);
  71.     headers.append('Authorization', 'Token dfacbbd0718c4f152e3914ff70be81f8c9a4c8f5');
  72.     let options = new RequestOptions({ headers: headers });
  73.     return headers;
  74.   }
  75.  
  76.   getDeclarations(): void {
  77.     let headers: Headers;
  78.     headers = this.getAuthorizationHeader();
  79.     let options = new RequestOptions({ headers: headers });
  80.     this.http.get('http://localhost:8000/backend/api/declarations/',
  81.       options)
  82.       .subscribe(response => {
  83.         this.declarations = response.json();
  84.         console.log(this.declarations);
  85.       });
  86.   }
  87.   ngOnInit() {
  88.     this.getDeclarations();
  89.     console.log(this.declarations);
  90.  
  91.   }
  92.  
  93. }
  94.  
  95.  
  96. //i get this fucking shit
  97. Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8000/backend/api/declarations/. (Reason: CORS preflight channel did not succeed).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement