2017-10-28 55 views
0

這是service.ts代碼。我使用服務器端REST API來驗證 用戶名和密碼。即使CORS出現在服務器端 之後,錯誤仍然存​​在。如果有人知道我在做什麼worng幫助我和 在此先感謝。使用Spring Boot Rest Api的Angular4中的Access-Control-Allow-Origin錯誤

import { Injectable } from '@angular/core'; 
import { Http, Headers, Response } from '@angular/http'; 
import { Observable } from 'rxjs/Rx'; 
import 'rxjs/add/operator/map'; 
import 'rxjs/add/operator/catch'; 
import 'rxjs/add/observable/throw'; 

@Injectable() 

export class Service { 

    constructor(private http: Http) { } 

    authenticate(model) { 
    var creds = "name=" + model.email + "&password=" + model.password; 
    console.log(creds) 
    var headers = new Headers(); 
    headers.append('Content-Type', 'application/json'); 

    return new Promise(resolve => { 
     this.http.post('http://sample.sample.com/api/authenticate', creds, {headers: headers}).subscribe(data => { 
      if(data.json().success){ 
       resolve(true); 
      } 
      else 
       resolve(false); 
     }); 
    }); 
    } 

    } 
+0

向我們展示'服務器端' –

回答

1
var creds = {"name":model.email,"password" : model.password}; 

您需要通過這樣的數據。

this.http.post('http://sample.sample.com/api/authenticate', JSON.stringify(creds), {headers: headers}).subscribe(data => { 
+0

謝謝你的回答對我來說工作正常 –

0

您需要在服務器上啓用CORS /在您的API中,CORS未啓用時會發生此錯誤。