douluohan3403 2018-12-17 11:28
浏览 61
已采纳

为什么我无法获得Angular的体形

I use angular to get information register html and I post to path api it work but print word "work" and I cant not get value In body and I test path post use postman it show firstname.......................................................................................................................................

server.go

package main

import (
    "github.com/gorilla/handlers"
    "github.com/gorilla/mux"
    "net/http"
)

func main() {

    router := mux.NewRouter()
    router.HandleFunc("/register", Createuser).Methods("POST")
    headers := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"})
    methods := handlers.AllowedMethods([]string{"GET", "POST", "PUT", "DELETE"})
    origins := handlers.AllowedOrigins([]string{"*"})
    http.ListenAndServe(":12345", handlers.CORS(headers, methods, origins)(router))

}


func Createuser(writer http.ResponseWriter, request *http.Request) {
    request.ParseForm()
    print(request.FormValue("firstName"))
    print("work")
}

register.component.ts

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { first } from 'rxjs/operators';

import { AlertService, UserService } from '../_services';

@Component({templateUrl: 'register.component.html'})
export class RegisterComponent implements OnInit {
    registerForm: FormGroup;
    loading = false;
    submitted = false;

    constructor(
        private formBuilder: FormBuilder,
        private router: Router,
        private userService: UserService,
        private alertService: AlertService) { }

    ngOnInit() {
        this.registerForm = this.formBuilder.group({
            firstName: ['', Validators.required],
            lastName: ['', Validators.required],
            username: ['', Validators.required],
            password: ['', [Validators.required, Validators.minLength(6)]]
        });
    }

    // convenience getter for easy access to form fields
    get f() { return this.registerForm.controls; }

    onSubmit() {
        this.submitted = true;

        // stop here if form is invalid
        if (this.registerForm.invalid) {
            return;
        }

        this.loading = true;
        this.userService.register(this.registerForm.value)
 
    }
}

user.service.ts

import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';

import {environment} from '../../environments/environment';
import {User} from '../_models';

@Injectable()
export class UserService {
  constructor(private http: HttpClient) {
  }


  register(user: User) {
     return this.http.post(`${environment.apiUrl}/register`, {
      firstName: user.firstName,
      lastName : user.lastName,
      username : user.username,
      password : user.password
    })
  }

  }
}

</div>
  • 写回答

1条回答 默认 最新

  • dpxbc88022 2018-12-17 11:43
    关注

    I think it could be that your go server is expecting a content-type: application/x-www-form-urlencoded

    https://golang.org/pkg/net/http/#Request.ParseForm

    But angular is submitting the form as a json object.

    To debug what is happening check the outgoing response from chrome to your backend Do this by enabling "Log XmlHttpRequest from the settings menu in chrome devtools.

    You should then be able to se that the body is JSON.

    Try using this to parse the body instead

    func test(rw http.ResponseWriter, req *http.Request) {
        decoder := json.NewDecoder(req.Body)
        var t test_struct
        err := decoder.Decode(&t)
        if err != nil {
            panic(err)
        }
        log.Println(t.Test)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥30 itest不允许查看成绩怎么办
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏