// 模拟ajax请求,author:908204694@qq.com
//
func Test_ajax(t *testing.T) {
urlTest := "http://www.xyz.com/ajax_url"
//传递的参数:
dataReader := strings.NewReader("qq=908204694")
newReq, err := http.NewRequest("POST", urlTest, dataReader)
if err != nil {
return
}
newReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
newReq.Header.Set("X-Requested-With", "XMLHttpRequest")
resp, err := http.DefaultClient.Do(newReq)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
}
//打印返回值
fmt.Println(string(body))
}