The following example shows how to properly construct both GET and POST requests against Web API 2 (CORS must be configured server-side, if sent from another domain):
<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.js"></script>
CORS with Windows Authentication test
<script type="text/javascript">
// GET
$.ajax({
url: "endpoint url here",
type: "GET",
dataType: "json",
xhrFields: {
withCredentials: true
}
})
.done(function (data, extra) {
alert("GET result" + JSON.stringify(data));
})
.fail(function(data, extra) {
});
//POST
$.ajax({
url: "url here",
type: "POST",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({testProp: "test value"}),
xhrFields: {
withCredentials: true
},
success: function(data) {
alert("POST success - " + JSON.stringify(data));
}
})
.fail(function(data) {
alert("Post error: " + JSON.stringify(data.data));
});
</script>
Server-side code:
[System.Web.Http.HttpGet]
[System.Web.Http.Route("GetRequestUsername")]
public HttpResponseMessage GetRequestUsername()
{
var ret = Request.CreateResponse(
HttpStatusCode.OK,
new { Username = SecurityService.GetUsername() });
return ret;
}
[System.Web.Http.HttpPost]
[System.Web.Http.Route("TestPost")]
public HttpResponseMessage TestPost([FromBody] object jsonData)
{
var ret = Request.CreateResponse(
HttpStatusCode.OK,
new { Username = SecurityService.GetUsername() });
return ret;
}