0

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.

3 Answers3

0

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

Eric
  • 1
  • it says page not found error 404 when compiled. and the terminal post an error when using this kind of url. the submit is a function under the controller and not a page. – Patrick Mark Mazo Apr 06 '16 at 01:20
0

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.

Uzbekjon
  • 11,655
  • 3
  • 37
  • 54
  • beego.Router("/sample", &controllers.SampleController{}) beego.Router("/sample/Submit",&controllers.SampleController{},"post:Submit") i have a defined route for post try to look – Patrick Mark Mazo Apr 05 '16 at 04:56
0

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