Golang http Server cross-domain problem resolution

Source: Internet
Author: User

Func Main () {Openhttplisten ()}func Openhttplisten () {http. Handlefunc ("/", Receiveclientrequest) fmt. PRINTLN ("Go server start running ...") Err: = http. Listenandserve (": 9090", nil) if err! = Nil {log. Fatal ("Listenandserve:", err)}}func receiveclientrequest (w http. Responsewriter, R *http. Request) {W.header (). Set ("Access-control-allow-origin", "*")             //Allow access to all domain W. Header (). ADD ("Access-control-allow-headers", "Content-type")//header the type W. Header (). Set ("Content-type", "Application/json")             //Return data format is JSONR. Parseform () fmt. Println ("Received client request:", R.form)

Test down, found that the web to send data to the Go server can be received, the go server returned data to the client Web side, there is a cross-domain error prompts, to HTTP. Responsewriter.header () Add these three lines of code:

W.header (). Set ("Access-control-allow-origin", "*")             //Allow access to all domain W. Header (). ADD ("Access-control-allow-headers", "Content-type")//header the type W. Header (). Set ("Content-type", "Application/json")             //Return Data format is JSON

Golang http Server cross-domain problem resolution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.