senden habe ich eine einfache Anwendung in Angular 2. Ich http.service.ts erstellt:Kann nicht POST von Angular2 zu Java
import {Injectable} from 'angular2/core';
import {Http} from 'angular2/http';
import {Observable} from "rxjs/Observable";
import 'rxjs/Rx';
import {Headers} from "angular2/http";
@Injectable()
export class HttpService {
constructor(private _http: Http){}
createPost(post: {title: string, body: string, userId: number}): Observable<any>{
const body = JSON.stringify(post);
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-urlencoded');
return this._http.post('http://localhost:8080/au', body, {
headers: headers
}).map(res => res.json());
}
}
Dann habe ich in app.component.ts einfache Eingaben mit einem botton:
Und in Java App erstellen Sie eine einfache Methode in meinem UserController mit @CrossOrigin
Annotation und erstellen innere Klasse Post.
@CrossOrigin(origins = "http://localhost:3000")
@RequestMapping(value = "/au", method = RequestMethod.POST)
public ResponseEntity<Void> au(@RequestBody Post post){
System.out.println(post.getBody() + " " + post.getTitle() + " " + post.getUserId());
return new ResponseEntity<Void>(HttpStatus.OK);
}
public class Post {
private String title, body;
private int userId;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
}
Aber wenn ich beide Apps starten, und klicken Sie Botton Post - Ich habe XMLHttpRequest cannot load
und HTTP status code 403
. Was muss ich sonst tun, um POST zu senden?
Was meinst du mit "verbinden"? Was ist das Problem? Erhalten Sie einen 'Access-Control-Allow -...' Fehler? –
Ich meine, ich habe eine Java-App mit DB. Wie kann ich in der Angular's2 Homepage (index.html) Login und Passwort von der DB schreiben und eingeloggt sein? –
Bearbeitet. Ich denke, "inject" wird korrekt sein, nicht "connect" –