13,347
社区成员
发帖
与我相关
我的任务
分享
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS " />
<add name="Access-Control-Allow-Headers" value="x-requested-with,content-type" />
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
2.前端代码
$("#save").click(function(){
jQuery.support.cors = true;
var jdata = {"access_token":"1231","createtime":"123","expires_in":"123"};
$.ajax({
url: 'http://192.168.1.81:8492/api/values/testPost',
type: "POST",
contentType:"application/json; charset=urf-8", //指定的是类型json串,不是 json 对象
dataType:'json',
data: JSON.stringify(jdata),
crossDomain: true,
success: function (returndata) {
alert(JSON.stringify(returndata));
},
error: function (returndata) {
alert(2);
}
});
});
3.后端api
[HttpPost, HttpOptions]
[ActionName("testPost")]
public ApiResult testPost()
{
try
{
// 接收
Stream inputstream = HttpContext.Current.Request.InputStream;
byte[] b = new byte[inputstream.Length];
inputstream.Read(b, 0, (int)inputstream.Length);
string inputstr = UTF8Encoding.UTF8.GetString(b);
return new ApiResult
{
IsError = false,
Msg = "获取成功",
PageCount = 1,
Result = inputstr
};
}
catch (Exception err)
{
return new ApiResult
{
IsError = true,
Msg = "失败",
PageCount = 1,
Result = "testPost"
};
}
}
4.答应结果