douzi115522 2018-11-19 23:56
浏览 74
已采纳

从登录名到服务器将用户名存储在数组中

I have a small Go web-server that displays data to users as they login. The problem I'm trying to achieve is to have the web-page only show certain information when a specific user logs in. For example, when admin logs in there would be a list of admin-only items that they can see on the web-page.

The problem I'm having is for some reason my Go code isn't storing the username in the array that I'm calling, so when I pass it over to the JavaScript it is blank.

Here are the 3 main parts of the code that I'm struggling with:

main.go

package main

import "fmt"

func authHandler(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    usernameArray, hasUsername := r.PostForm["j_username"]

    //This line added for debugging purposes
    log.Println("username:", usernameArray[0])

    if hasUsername {
        fmt.Fprintf(w, "%s", usernameArray[0])
    }
}

func main() {
    http.HandleFunc("/getAuth", authHandler)
}

javascript.js

Note that this is AngularJS

$scope.init = function() {
    checkAuthentication();
};

checkAuthentication = function() {
    $http.get("/getAuth").then(
    function(response) {
        var username = response.data;

        console.log(username): //Added for debugging purposes

        if (username === "admin") {
            $scope.showAdminOnlyItems = true;
        }
    });
}

main.html

<div id="admin-only-items" ng-show="showAdminOnlyItems">
    <hr style="border:1px solid">
    <p style="text-align: center">admin only: </p>
    <div id="adminOnlyButtom">
        <button class="button" data-ng-click="doSomething()">Do something</button>
    </div>
</div>

Again, I only want that div to show up when admin logs in, and Go needs to send the username over to the javascript to verify that. By adding the debugging line in Go and starting the server up, I get this:

2018/11/19 16:28:42 http: panic serving 10.240.49.238:59621: runtime error: 
index out of range
goroutine 26 [running]:
net/http.(*conn).serve.func1(0xc42009f720)
        C:/Go/src/net/http/server.go:1726 +0xd0
panic(0x79f820, 0xe17940)
        C:/Go/src/runtime/panic.go:502 +0x229
main.authHandler(0xc56220, 0xc420135180, 0xc4207a3500)
        D:/src/main.go:346 +0x1d8

So it's clear that the usernameArray is empty, not sure what I did wrong. Can anyone help tell me why the usernameArray is empty in authHandler?

  • 写回答

1条回答 默认 最新

  • dsgw3315 2018-11-20 02:16
    关注

    The first, I can see that you send GET request to server without j_username query param therefore you can not read j_username on server side.

    The second, usernameArray is empty slice which is failing while parse j_username. Error index out of range occur when you try to call usernameArray[0].

    You should send GET request with j_username like that /getAuth?j_username=admin and modify code from server.

        usernameArray, hasUsername := r.URL.Query()["j_username"]
        //This line added for debugging purposes
        log.Println("debug here :", usernameArray)
    
        if hasUsername {
            fmt.Fprintf(w, "%s", usernameArray[0])
            return
        }
    
        // Send an error message to client
        http.Error(w, `missing username`, 500)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 qtcp 发送数据时偶尔会遇到发送数据失败?用的MSVC编译器(标签-qt|关键词-tcp)
  • ¥15 cam_lidar_calibration报错
  • ¥15 拓扑学,凸集,紧集。。
  • ¥15 如何扩大AIS数据容量
  • ¥15 单纯型python实现编译报错
  • ¥15 c++2013读写oracle
  • ¥15 c++ gmssl sm2验签demo
  • ¥15 关于模的完全剩余系(关键词-数学方法)
  • ¥15 有没有人懂这个博图程序怎么写,还要跟SFB连接,真的不会,求帮助
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因