不允许使用AngularJS和Go POST请求方法

Im currently working on a sample project and Im using Go and AngularJS I am new at this. I encountered a 405 Error Method Not Allowed after executing this codes.

sample.js

var app = angular.module('sample', []);
app.controller('sampleCtrl', function($scope, $http){
  $scope.submit = function(){
    //variables
    $scope.firstName = document.getElementById('firstName').value;
    $scope.middleName = document.getElementById('middleName').value;
    $scope.lastName = document.getElementById('lastName').value;
    $scope.age = document.getElementById('age').value;
    $http({
            method: 'POST',
            url: baseUrl +'/sample',
            headers: {'Content-Type': 'application/json'},
            data: {
              "firstName"   : $scope.firstName,
              "middleName"  : $scope.middleName,
              "lastName"    : $scope.lastName,
              "age"         : $scope.age
            }
        }).then(function successCallback(response){
              alert('Success');
      });
    }
});

sample.go

package controllers

import (
    "github.com/astaxie/beego"
    "net/http"
    "fmt"
    "encoding/json"
)
type SampleController struct {
    beego.Controller
}

func (this *SampleController) Get(){
    this.TplName = "sample/sample.html"
  this.Render()
}

type Entry struct {
 FirstName string
MiddleName string
  LastName string
    Age int
}

func (this *SampleController) Submit(rw http.ResponseWriter, req *http.Request){
    decoder := json.NewDecoder(req.Body)
    var data Entry
    err := decoder.Decode(&data)
    if err != nil {
        fmt.Println("JSON Empty")
    }else{
        var firstName = data.FirstName
        //var middle = data.MiddleName
        //var lastName = data.LastName
        //var age = data.Age
        fmt.Println(firstName)
    }
}

routers.go

package routers

import (
    "test/controllers"
    "github.com/astaxie/beego"
)

func init() {
    beego.Router("/", &controllers.MainController{})
    beego.Router("/sample", &controllers.SampleController{})    beego.Router("/sample/Submit",&controllers.SampleController{},"post:Submit")
}

Thanks for the help in advance.

</div>

Remove baseUrl and make sure url:"sample". Maybe you can do this console.log(baseUrl); Check that baseUrl contains #;

I am not a Go developer, but looking at the error code it seems like you are making POST request, but have only defined routes for GET.

In router u have defined "/sample" as GET but you made an ajax call for POST method, its search's in the router for /sample it will find this

beego.Router("/sample", &controllers.SampleController{})

which redirects to SampleController but there it doesn't find any POST method definition so 405 method not found.

Try adding in samplecontroller

func (this *SampleController) Post(){ ...// your code goes here }

or add

beego.Router("/sample", &controllers.SampleController{"post:Sample"})

and add a Function Sample in samplecontroller just as you did for Submit