寫伺服器遇到問題了,求大神幫幫

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

// 這是個8020連接埠伺服器 問題是用瀏覽器能訪問,使用ajax訪問卻訪問不了,報錯:localhost:8020/sayhello. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

 

package main

import (
    "database/sql"
    "fmt"
    _ "github.com/go-sql-driver/mysql"
    "net/http"
    "net/url"
)

/**
 * @Author:      YaoWang
 * @DateTime:    2017-04-11 10:09:15
 * @Description: 返回資訊
 */
func sayHelloName(w http.ResponseWriter, r *http.Request) {
    //r.Method 判斷是什麼請求類型
    if r.Method == "GET" {
        fmt.Fprintf(w, r.Method+"hello, world 這是get請求!!")
    } else {
        fmt.Fprintf(w, r.Method+"hello, world 這是其他形式請求!!")
    }
    //這種方式不能處理get和post請求出現相同參數名的情況
    r.ParseForm()
    // fmt.Println(r.Form["username"])
    //解析傳過來的參數
    // fmt.Println(r.URL.RawQuery)
    queryForm, err := url.ParseQuery(r.URL.RawQuery)
    if err == nil && len(queryForm["username"]) > 0 {
        fmt.Println(r.Form["username"])
    }
    //當出現src中含有mysql驅動卻還是報錯的時候 刪除原有包 重新get一下 解決問題go get github.com/go-sql-driver/mysql
    db, err := sql.Open("mysql", "beego:123456@/qscrm")
    defer db.Close()
    if err != nil {
        fmt.Println("開啟資料庫失敗!!")
    }
    row, err := db.Query("select * from user_info")
    if err != nil {
        fmt.Println("查詢資料庫失敗!!")
    } else {
        var id int = 0
        var user_name string = ""
        var user_pwd string = ""
        var user_group_id int = 0
        var user_group_name string = ""
        for row.Next() {
            row.Scan(&id, &user_name, &user_pwd, &user_group_id, &user_group_name)
            fmt.Println("id:", id, ",name:", user_name, id, ",user_pwd:", user_pwd, "user_group_id:", user_group_id, "user_group_name:", user_group_name)
        }
    }

}

/**
 * @Author:      YaoWang
 * @DateTime:    2017-04-11 10:05:54
 * @Description: 主程式
 */
func main() {
    //http接收handle資訊 返回資訊
    http.HandleFunc("/sayhello", sayHelloName)

    http.HandleFunc("/login", userLogin)
    //監聽連接埠
    http.ListenAndServe(":8020", nil)

}

/**
 * @Author:      YaoWang
 * @DateTime:    2017-04-12 14:06:15
 * @Description: 使用者登入
 */
func userLogin(w http.ResponseWriter, r *http.Request) {
    fmt.Println("userLogin========")
    //加了頭資訊 ,既可以訪問 跨域請求(解決方案)
    w.Header().Add("Access-Control-Allow-Origin", "*")
    if r.Method == "GET" {
        fmt.Fprintf(w, r.Method+"hello, world 這是get請求!!")
    } else {
        fmt.Fprintf(w, r.Method+"hello, world 這是其他形式請求!!")
    }
}
 

729 次點擊  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.