AJAX不可以实现跨域请求,经过特殊处理才行。一般后台可以通过WebClient实现跨域请求~
//get 请求
string url = string.Format("http://localhost:28450/api/values?str1=a&str2=b");
WebClient wc = new WebClient();
Encoding enc = Encoding.GetEncoding("UTF-8");
Byte[] pageData = wc.DownloadData(url);DownloadString(url);
string re = enc.GetString(pageData);
//post 请求(注意Post请求都是传递Json过去)
Person p=new Person(){Name="张三",Age=22};
string postData = Convert.Serialize(p);
byte[] bytes = Encoding.UTF8.GetBytes(postData);
WebClient client = new WebClient();
client.Headers.Add("Content-Type", "application/json");
client.Headers.Add("ContentLength", postData.Length.ToString());
Encoding enc = Encoding.GetEncoding("UTF-8");
byte[] responseData = client.UploadData("http://localhost:28450/api/values", "POST", bytes);
string re = enc.GetString(responseData);
//调用接口Demo
string url = "http://localhost:62691/SessionState/TransmitSession";//获取会话同步接口 //调用接口进行同步 string timeStamp = GetTimeStamp();//获取时间戳 string sign = DESEncrypt.Encrypt(timeStamp, "dxqmp52c0esxymru"); Person p=new Person(){Name="张三",Age=22,Gender="男"}; string postData =p; byte[] bytes = Encoding.UTF8.GetBytes(postData); WebClient client = new WebClient(); client.Headers.Add("Content-Type", "application/json"); client.Headers.Add("ContentLength", postData.Length.ToString()); Encoding enc = Encoding.GetEncoding("UTF-8"); byte[] responseData = client.UploadData(url, "POST", bytes); string re = enc.GetString(responseData);