我使用vS2012创建了一个mvc4 web api项目.我使用以下教程来解决跨源资源共享问题,"http://blogs.msdn.com/b/carlosfigueira/archive/2012/07/02/cors-support-in-asp-net-web-api- RC-version.aspx".它运行成功,我成功地将数据从客户端发布到服务器.
在我的项目中实现Autherization之后,我使用以下教程来实现OAuth2,"http://community.codesmithtools.com/CodeSmith_Community/b/tdupont/archive/2011/03/18/oauth-2-0-for -mvc两足-implementation.aspx".这有助于我在客户端获取RequestToken.
但是当我从客户端发布数据时,我得到了错误, "XMLHttpRequest无法加载http://.请求头字段Access-Control-Allow-Headers不允许Content-Type."
我的客户端代码看起来像,
function PostLogin() {
var Emp = {};
Emp.UserName = $("#txtUserName").val();
var pass = $("#txtPassword").val();
var hash = $.sha1(RequestToken + pass);
$('#txtPassword').val(hash);
Emp.Password= hash;
Emp.RequestToken=RequestToken;
var createurl = "http://localhost:54/api/Login";
$.ajax({
type: "POST",
url: createurl,
contentType: "application/json; charset=utf-8",
data: JSON.stringify(Emp),
statusCode: {
200: function () {
$("#txtmsg").val("done");
toastr.success('Success.', '');
}
},
error:
function (res) {
toastr.error('Error.', 'sorry either your username of password was incorrect.');
}
});
};
Run Code Online (Sandbox Code Playgroud)
我的api控制器看起来像,
[AllowAnonymous] …Run Code Online (Sandbox Code Playgroud)