我已閱讀:爲什麼我不需要JSON.stringify()這個帖子的有效載荷?
How to post json object with Http.post (Angular 2) (php server side)
How do I POST JSON in Angular 2?
而且兩者建議創建頭,做一個POST請求,其工作之前stringfy我的對象到JSON,但我注意到,如果我只是通過它的工作原理也很好。
所以我只是想知道誰是這種行爲的責任,因爲我相信一些圖層將對象轉換爲JSON格式。或不?
我使用:
- 角4的前端。
- Wildfly 10.1.0最終
- RestEasy的3.1.1最終
我有一個person.component.ts以下方法:
addPerson() {
this.personService.save(this.person)
.then(result => {
console.log('Added!', result);
});
}
一個人.service.ts與保存方法:
save(person: Person){
//OPTION 3
return this.http.post(`${this.baseURL}person/add`, person).toPromise();
}
後端我有我的REST應用類:
@ApplicationPath("/rest")
public class HouseApplication extends Application {
public HouseApplication() {}
}
和我一個人的資源:
@RequestScoped
@Path("/person")
@Produces({ "application/json" })
@Consumes({ "application/json" })
public class PersonResource {
public PersonResource() {}
@Inject
PersonService personService;
@POST
@Path("/add")
public void addPerson(Person person) throws HouseValidationException{
personService.add(person);
}
...
我真的開始在前端的例外,但我得到的記錄持續到數據庫。我覺得例外是涉及到我使用的按鈕,或者在我的屏幕上PRIMEng UI組件......
我在我的控制檯以下消息後來電:
在ERROR Error: Uncaught (in promise): Unexpected literal at position 2
at resolvePromise (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:710:31) [angular]
at eval (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:761:17) [angular]
at Object.onInvokeTask (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4322:37) [angular]
at drainMicroTaskQueue (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:591:35) [<root>]
at HTMLButtonElement.ZoneTask.invoke (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:464:25) [<root>]
at ____________________Elapsed_3_ms__At__Fri_May_05_2017_02_45_38_GMT_0300__BRT_ (http://localhost)
at getStacktraceWithUncaughtError (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:39:12) [angular]
at new LongStackTrace (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:33:22) [angular]
at Object.onScheduleTask (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:109:22) [angular]
at Object.onScheduleTask (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:274:29) [angular]
at scheduleResolveOrReject (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:756:14) [angular]
at ZoneAwarePromise.then (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:845:17) [angular]
at NgModel._updateValue (eval at <anonymous> (http://localhost:8080/house/vendor.js:72:1), <anonymous>:4434:27) [angular]
at NgModel.ngOnChanges (eval at <anonymous> (http://localhost:8080/house/vendor.js:72:1), <anonymous>:4316:18) [angular]
at checkAndUpdateDirectiveInline (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:10907:19) [angular]
at checkAndUpdateNodeInline (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12289:17) [angular]
at checkAndUpdateNode (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12257:16) [angular]
at debugCheckAndUpdateNode (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12886:59) [angular]
at debugCheckDirectivesFn (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12827:13) [angular]
at Object.eval [as updateDirectives] (ng:///AppModule/PatientComponent.ngfactory.js:2577:5) [angular]
at Object.debugUpdateDirectives [as updateDirectives] (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12812:21) [angular]
at checkAndUpdateView (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12224:14) [angular]
at callViewAction (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12539:17) [angular]
at execComponentViewsAction (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12485:13) [angular]
at checkAndUpdateView (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12230:5) [angular]
at callViewAction (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12539:17) [angular]
at execComponentViewsAction (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12485:13) [angular]
at checkAndUpdateView (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12230:5) [angular]
at callWithDebugContext (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:13212:42) [angular]
at Object.debugCheckAndUpdateView [as checkAndUpdateView] (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12752:12) [angular]
at ViewRef_.detectChanges (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:10321:63) [angular]
at eval (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:5258:63) [angular]
at Array.forEach (native) [angular]
at ApplicationRef_.tick (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:5258:25) [angular]
at eval (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:5138:106) [angular]
at Object.onInvoke (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4331:37) [angular]
at NgZone.run (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4200:62) [angular]
at Object.next (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:5138:82) [angular]
at SafeSubscriber.schedulerFn [as _next] (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4034:52) [angular]
at SafeSubscriber.__tryOrUnsub (eval at <anonymous> (http://localhost:8080/house/vendor.js:28:1), <anonymous>:236:16) [angular]
at SafeSubscriber.next (eval at <anonymous> (http://localhost:8080/house/vendor.js:28:1), <anonymous>:185:22) [angular]
at Subscriber._next (eval at <anonymous> (http://localhost:8080/house/vendor.js:28:1), <anonymous>:125:26) [angular]
at Subscriber.next (eval at <anonymous> (http://localhost:8080/house/vendor.js:28:1), <anonymous>:89:18) [angular]
at EventEmitter.Subject.next (eval at <anonymous> (http://localhost:8080/house/vendor.js:79:1), <anonymous>:55:25) [angular]
at EventEmitter.emit (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4020:76) [angular]
at NgZone.checkStable (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4296:40) [angular]
at NgZone.onLeave (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4372:14) [angular]
at Object.onInvokeTask (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4325:27) [angular]
at HTMLButtonElement.ZoneTask.invoke (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:460:38) [<root>]
at ____________________Elapsed_18597_ms__At__Fri_May_05_2017_02_45_19_GMT_0300__BRT_ (http://localhost)
at getStacktraceWithUncaughtError (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:39:12) [angular]
at new LongStackTrace (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:33:22) [angular]
at Object.onScheduleTask (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:109:22) [angular]
at Object.onScheduleTask (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:274:29) [angular]
at zoneAwareAddListener (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:1453:14) [angular]
at HTMLButtonElement.addEventListener (eval at createNamedFn (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1)), <anonymous>:3:43) [angular]
at DomEventsPlugin.addEventListener (eval at <anonymous> (http://localhost:8080/house/vendor.js:186:1), <anonymous>:3131:17) [angular]
at EventManager.addEventListener (eval at <anonymous> (http://localhost:8080/house/vendor.js:186:1), <anonymous>:2477:23) [angular]
at EmulatedEncapsulationDomRenderer2.DefaultDomRenderer2.listen (eval at <anonymous> (http://localhost:8080/house/vendor.js:186:1), <anonymous>:2990:36) [angular]
at DebugRenderer2.listen (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:13476:30) [angular]
at listenToElementOutputs (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:9558:66) [angular]
at createViewNodes (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12148:17) [angular]
at callViewAction (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12546:13) [angular]
at execComponentViewsAction (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12485:13) [angular]
at createViewNodes (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12203:5) [angular]
at callViewAction (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12546:13) [angular]
at execComponentViewsAction (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12485:13) [angular]
at createViewNodes (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12203:5) [angular]
at createRootView (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12081:5) [angular]
at callWithDebugContext (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:13212:42) [angular]
at Object.debugCreateRootView [as createRootView] (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:12673:12) [angular]
at ComponentFactory_.create (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:10017:46) [angular]
at ComponentFactoryBoundToModule.create (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:3633:29) [angular]
at ApplicationRef_.bootstrap (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:5215:57) [angular]
at eval (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:5002:79) [angular]
at Array.forEach (native) [angular]
at PlatformRef_._moduleDoBootstrap (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:5002:42) [angular]
at eval (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4964:27) [angular]
at Object.onInvoke (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4331:37) [angular]
at eval (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:758:57) [angular]
at Object.onInvokeTask (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4322:37) [angular]
at drainMicroTaskQueue (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:591:35) [<root>]
at <anonymous> [<root>]
at ____________________Elapsed_62_ms__At__Fri_May_05_2017_02_45_19_GMT_0300__BRT_ (http://localhost)
at getStacktraceWithUncaughtError (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:39:12) [angular]
at new LongStackTrace (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:33:22) [angular]
at Object.onScheduleTask (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:109:22) [angular]
at Object.onScheduleTask (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:274:29) [angular]
at scheduleResolveOrReject (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:756:14) [angular]
at resolvePromise (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:705:21) [angular]
at eval (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:758:17) [angular]
at Object.onInvokeTask (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4322:37) [angular]
at drainMicroTaskQueue (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:591:35) [<root>]
at <anonymous> [<root>]
at ____________________Elapsed_4_ms__At__Fri_May_05_2017_02_45_19_GMT_0300__BRT_ (http://localhost)
at getStacktraceWithUncaughtError (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:39:12) [angular]
at new LongStackTrace (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:33:22) [angular]
at Object.onScheduleTask (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2776:1), <anonymous>:109:22) [angular]
at Object.onScheduleTask (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:274:29) [angular]
at scheduleResolveOrReject (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:756:14) [angular]
at ZoneAwarePromise.then (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:845:17) [angular]
at new ApplicationInitStatus (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:3120:60) [angular]
at AppModuleInjector.createInternal (ng:///AppModule/module.ngfactory.js:167:35) [angular]
at AppModuleInjector.NgModuleInjector.create (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:3743:76) [angular]
at NgModuleFactory.create (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:3716:18) [angular]
at eval (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4954:61) [angular]
at Object.onInvoke (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4331:37) [angular]
at NgZone.run (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4200:62) [<root>]
at PlatformRef_._bootstrapModuleFactoryWithZone (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4952:23) [<root>]
at eval (eval at <anonymous> (http://localhost:8080/house/vendor.js:14:1), <anonymous>:4993:59) [<root>]
at eval (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:758:57) [<root>]
at drainMicroTaskQueue (eval at <anonymous> (http://localhost:8080/house/polyfills.js:2782:1), <anonymous>:591:35) [<root>]
at <anonymous> [<root>]
defaultErrorLogger @ core.es5.js?0445:1290
ErrorHandler.handleError @ core.es5.js?0445:1350
next @ core.es5.js?0445:4960
schedulerFn @ core.es5.js?0445:4034
SafeSubscriber.__tryOrUnsub @ VM28777:236
SafeSubscriber.next @ VM28777:185
Subscriber._next @ VM28777:125
Subscriber.next @ VM28777:89
Subject.next @ VM28804:55
EventEmitter.emit @ core.es5.js?0445:4020
NgZone.triggerError @ core.es5.js?0445:4391
onHandleError @ core.es5.js?0445:4352
ZoneDelegate.handleError @ zone.js?fad3:369
Zone.runGuarded @ zone.js?fad3:141
_loop_1 @ zone.js?fad3:602
drainMicroTaskQueue @ zone.js?fad3:611
ZoneTask.invoke @ zone.js?fad3:464
person.component.ts?10bd:76 '
Added! Response {_body: null, status: 204, ok: true, statusText: "No Content", headers: Headers…}
注控制檯消息的結尾,我已記錄消息'已添加!'和響應這很好。
如此反覆,爲什麼我沒有需要 stringfy我 人對象,甚至也沒有設置 的Content-Type到 應用/ JSON價值,但它得到了由REST端點,它應該明白只消費應用程序/ json內容?
任何想法?我錯過了什麼?
裏面我看到的,所以角正在處理這個問題,讓它休息不是更好嗎?它可能會以錯誤的方式「教導」新手,或者更多的不完整的方式...... –
@skinny_jones我明白你的觀點。但是,一些新手會感到沮喪,並在他們看到紅線時放棄框架。我相信你知道我的意思;) – echonax