前台按钮
<button class="btn btn-primary" style="margin-left:20px" onclick="Get1();" );">测试GET接口1</button>
<button class="btn btn-primary" style="margin-left:20px" onclick="Get2();" );">测试GET接口2</button>
js代码
<script>
function Get1() {
$.get("/Teacher/Get",{ "id": "小黑" }, function (answer) {
console.log(answer);
console.log("1");
});
}
function Get2() {
$.ajax({
url: "/Teacher/Get",
type: "get",
data: { "id": "aaa" },
success: function (data) {
console.log(data);
console.log("2");
}
})
}
</script>
后台代码1
public ActionResult Get(string id)
{
string url = "http://api.nnzhp.cn/api/user/stu_info?stu_name='" + id + "'";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
request.UserAgent = null;
request.Timeout = 6000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
if (retString != "")
{
return this.Content(retString);
}
else
{
return this.RedirectToAction("list");
}
}
后台代码2
[HttpGet]
public JsonResult GetImportDriverList( int pageIndex, string companyId = "")
{
string url = "http://116.255.145.168:8001/Api/GetDriverList?companyId=" + companyId + "&pageIndex=" + pageIndex + "";
WebRequest wRequest = WebRequest.Create(url);
wRequest.Method = "GET";
wRequest.ContentType = "text/html;charset=UTF-8";
WebResponse wResponse = wRequest.GetResponse();
Stream stream = wResponse.GetResponseStream();
StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8);
string str = reader.ReadToEnd(); //url返回的值
reader.Close();
wResponse.Close();
JObject jo = (JObject)JsonConvert.DeserializeObject(str);
return AjaxJsonResult.SuccessResult(new { data = jo }, true);
}