这是我的Service Typscript,我有Json post
方法 import {Injectable, Inject} from "angular2/core";
import {Http, Headers} from "angular2/http";
import 'rxjs/add/operator/map';
import {User} from "../User/User";
@Injectable()
export class loginService {
constructor(@Inject(Http) private _http:Http) {
}
enter code here
postJSON(){
var json = JSON.stringify({v:"value",a:"3"}); // var creds = "username=" + "khalil" + "&password=" + 3; .map(res=>res.text());
var headers = new Headers();
headers.append('Content-Type','application/json');
return this._http.post('http://localhost:8080/connect',json,{
headers:headers
})
.map(res=>res.json());
}
这是我订阅的组件
import {Component, Inject} from 'angular2/core';
import {Router} from "angular2/router";
import {loginService} from "./login.service";
import {HTTP_PROVIDERS, Http} from "angular2/http";
import {User} from "../User/User";
@Component({
selector: 'login',
templateUrl: '../dev/login/login.html',
providers: [HTTP_PROVIDERS, loginService]
})
export class TestComponent {
getData:string;
postData:string;
constructor(private _router:Router,
@Inject(Http) http:Http,
private _loginService:loginService) {
}
postJson(){
this._loginService.postJSON().subscribe(
data => this.postData = JSON.stringify(data),
error => alert(this.postData + error),
() => console.log("finished")
);
}
}
}
服务器端我使用spring 4
这是我的控制器
@RestController
@CrossOrigin()
public class HomeController {
@RequestMapping(value = "/connect", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String connect(String v, String a) {
int i = Integer.parseInt(a);
if (v.equals("khalil") && i == 3) {
System.out.println("----------------------***********************---------------------");
} else {
System.out.println("----------------------**** elseeeeee *******************---------------------");
}
return v+a;
}
}
当我尝试调用Web服务时,我收到此错误
POST http://localhost:8080/connect 500 (Internal Server Error)(anonymous function)
@ http.js:683Observable.subscribe
@ Rx.js:11006Observable._subscribe
@ Rx.js:11038Observable.subscribe
@ Rx.js:11004loginComponent.postJson
@ login.component.ts:63ChangeDetector_loginComponent_0.handleEventInternal
@ viewFactory_loginComponent:415AbstractChangeDetector.handleEvent
@ angular2.js:9568AppView.triggerEventHandlers
@ angular2.js:10246(anonymous function)
@ viewFactory_loginComponent:558(anonymous function)
@ angular2.js:14068(anonymous function)
@ angular2.js:13496ZoneDelegate.invoke
@ angular2-polyfills.js:332NgZoneImpl.inner.inner.fork.onInvoke
@ angular2.js:2111ZoneDelegate.invoke
@ angular2-polyfills.js:331Zone.runGuarded
@ angular2-polyfills.js:241NgZoneImpl.runInner
@ angular2.js:2140NgZone.run
@ angular2.js:13649outsideHandler
@ angular2.js:13495ZoneDelegate.invokeTask
@ angular2-polyfills.js:365Zone.runTask
@ angular2-polyfills.js:263ZoneTask.invoke
@ angular2-polyfills.js:431
感谢帮助我^^