处理CORS表单提交

时间:2016-03-17 00:54:30

标签: html forms go

我有一个非常简单的表格,来自localhost:3000

    public func retrieveAccessiblePhoneNumber(phoneNumber: String) -> String {
    // We want to know if a character is a number or not
    let characterSet = NSCharacterSet(charactersInString: "0123456789")

    // We use this formatter to spell out individual numbers
    let numberFormatter = NSNumberFormatter()
    numberFormatter.numberStyle = .SpellOutStyle

    var spelledOutComponents = [String]()
    let range = Range<String.Index>(start: phoneNumber.startIndex, end: phoneNumber.endIndex)

    // Loop over the phone number add add the accessible variants to the array
    phoneNumber.enumerateSubstringsInRange(range,
        options: NSStringEnumerationOptions.ByComposedCharacterSequences) { (substring, substringRange, enclosingRange, stop) -> () in
            // Check if it's a number
            if let substr = substring where substr.rangeOfCharacterFromSet(characterSet) != nil {
                if let number = Int(substr) {
                    // Is a number
                    let nsNumber = NSNumber(integer: number)
                    spelledOutComponents.append(numberFormatter.stringFromNumber(nsNumber)!)
                }
            } else {
                // Is not a number
                spelledOutComponents.append(substring!)
            }
    }

    // Finally separate the components with spaces (so that the string doesn't become "ninefivesixfive".
    return spelledOutComponents.joinWithSeparator(" ")
}

在localhost:8080上,我有一个非常简单的go服务器:

<form id="my-HTML-form" action="http://localhost:8080" method="POST">
    <input type="text" placeholder="Username" name="username" />
    <input type="password" placeholder="Password" name="password" />
    <input type="hidden" name="form-id" value="login" />
    <button type="submit">Submit</button>
</form>

当我提交表单时,我实际收到两个请求!一个POST和一个GET!这是我提交的单一提交后的控制台:

package main

import (
    "log"
    "net/http"
)

func main() {
    // Start the server
    http.HandleFunc("/", handler)
    serverErr := http.ListenAndServe(":8080", nil)
    if serverErr != nil {
        log.Println("Error starting server")
        log.Fatal(serverErr)
    }
}

func handler(w http.ResponseWriter, r *http.Request) {
    log.Println(r.Header.Get("Origin"))
    log.Println(r.Method)

    w.Header().Set("Access-Control-Allow-Origin", "*")
    w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
    w.Header().Set("Access-Control-Allow-Headers",
    "Accept, Accept-Encoding, Authorization, Content-Length, Content-Type, Origin, X-CSRF-Token")
    w.WriteHeader(http.StatusOK)
}

请注意,GET请求没有附加原点。我试图执行一些逻辑,然后根据成功或失败将用户重定向到不同的URL。但我无法做到这一点,因为GET请求紧跟POST请求。我可以使用AJAX,没问题,但我希望找到一个简单的html表单提交的解决方案。

任何想法,想法?所有浏览器都遵循POST/Redirect/Get范例和我的SOL吗?

感谢。

1 个答案:

答案 0 :(得分:1)

我认为您的表单操作是action="http://localhost:8080",因为您说这是一个跨源请求。

第二个GET请求是对favicon的请求(正如elithrar在评论中指出的那样)。只需要log.Println(r.URL)来确保。我不知道为什么浏览器不会为它添加原始标题。

您可以通过替换w.WriteHeader(http.StatusOK)来重定向请求,例如, http.Redirect(w, r, "http://localhost:3000/success.html", http.StatusSeeOther)