golang http服务器跨域问题解决
2016-04-12 13:45
393 查看
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", "*") //允许访问所有域 w.Header().Add("Access-Control-Allow-Headers", "Content-Type") //header的类型 w.Header().Set("content-type", "application/json") //返回数据格式是json r.ParseForm() fmt.Println("收到客户端请求: ", r.Form)
测试下来,发现web端发送数据给go服务器是可以收到的,就在go服务器返回数据给client web端的时候,出现了跨域错误提示,给http.ResponseWriter.Header() 加这三行代码即可:
w.Header().Set("Access-Control-Allow-Origin", "*") //允许访问所有域 w.Header().Add("Access-Control-Allow-Headers", "Content-Type") //header的类型 w.Header().Set("content-type", "application/json") //返回数据格式是json
相关文章推荐
- 神经网络第一部分:背景Neural Networks, Part 1: Background
- 神经网络第二部分:神经元Neural Networks, Part 2: The Neuron
- 神经网络第三部分:网络Neural Networks, Part 3: The Network
- 神经网络第三部分:网络Neural Networks, Part 3: The Network
- 【ZOJ3933 The 16th Zhejiang University Programming ContestG】【费用流】Team Formation 最多组队条件下女
- PHP CURL HTTP研究
- 从贝叶斯方法谈到贝叶斯网络
- java.lang.ClassCastException: libcore.net.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl
- 深度神经网络入门教程Deep Neural Networks: A Getting Started Tutorial
- 神经网络指南Hacker's guide to Neural Networks
- 学Http协议
- 用matlab训练数字分类的深度神经网络Training a Deep Neural Network for Digit Classification
- 卷积神经网络Convolutional Neural Networks
- 深度卷积神经网络用于图像缩放Image Scaling using Deep Convolutional Neural Networks
- http://与https://的区别
- 通过静态广播监听网络变化,在通过回调通知
- 网络流题目泛做(费用流的不写在这里面)
- PXE网络装机(有人值守与无人值守安装)
- Linux网络配置和MySQL、Apache、PHP环境的搭建
- 网络编程--ASI--(ASIHTTPRequest)介绍