Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何向“发布json请求并接收json响应”;go服务器";(go语言)使用java脚本_Javascript_Json_Http_Go_Xmlhttprequest - Fatal编程技术网

Javascript 如何向“发布json请求并接收json响应”;go服务器";(go语言)使用java脚本

Javascript 如何向“发布json请求并接收json响应”;go服务器";(go语言)使用java脚本,javascript,json,http,go,xmlhttprequest,Javascript,Json,Http,Go,Xmlhttprequest,如何使用java脚本发布json请求并从“go服务器”(go语言)接收json响应 我试过这个 java脚本代码: var calculate = { operand1 : null, operand2 : null, operator : null }; function UserAction() { var xhttp = new XMLHttpRequest(); xhttp

如何使用java脚本发布json请求并从“go服务器”(go语言)接收json响应

我试过这个

java脚本代码:

var calculate = { 
                operand1 : null,
                operand2 : null,
                operator : null
};

function UserAction() {
    var xhttp = new XMLHttpRequest();
    xhttp.open("POST", "http://localhost:8000/", true);
    xhttp.setRequestHeader("Content-type", "application/json");
    xhttp.send(calculate);
    var response = (xhttp.responseText);
    console.log(response);
}
UserAction();
go代码:

package main
import ("fmt"
        "net/http"
        "encoding/json"
)


type answer struct {
    result float64
}


func index(w http.ResponseWriter, r *http.Request) {
    ans := answer{result: 30}
    fmt.Println(r)
    w.Header().Set("Content-Type", "application/json; charset=UTF-8")
    w.WriteHeader(http.StatusOK)
    if err := json.NewEncoder(w).Encode(ans); err != nil {
        panic(err)
    }    
}

func main() {
    http.HandleFunc("/",index)
    fmt.Println("Server online at port localhost:8000")
    http.ListenAndServe(":8000", nil)

}
我收到一条错误信息

未能加载:对飞行前请求的响应 未通过访问控制检查:无“访问控制允许来源” 请求的资源上存在标头。原点“null”为空 因此不允许访问


在您的Golang代码中设置
Access Control Allow Origin
头,以允许请求表单跨Origin

func index(w http.ResponseWriter, r *http.Request) {
    ans := answer{result: 30}
    fmt.Println(r)
    w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
    w.Header().Set("Content-Type", "application/json; charset=UTF-8")
    w.Header().Set("Access-Control-Allow-Origin", "*")
    w.WriteHeader(http.StatusOK)
    if err := json.NewEncoder(w).Encode(ans); err != nil {
        panic(err)
    }    
}
此外,您还可以使用CORS处理程序来处理通过主路由器的每个请求

func main() {
    mux := http.NewServeMux()
    mux.HandleFunc("/",index)
    handler := cors.Default().Handler(mux)
    fmt.Println("Server online at port localhost:8000")
    http.ListenAndServe(":8000", handler)
}

更改加载失败后:飞行前响应中的Access Control Allow Headers不允许请求标头字段内容类型我们需要设置标头以允许选项
Access Control Allow Headers
您能告诉我如何处理发送和接收的数据吗?我只想将其转换为variables@akashsc那是另一个问我的代码是否有效。请接受它,并张贴另一个问题与您的代码,我们一定会帮助您。