I need to use my company's authentication service, which runs on a remote server. I've been trying to do an ajax call to the service(API) using jquery AJAX as follows....
var uri = "http://Company/Authentication/AuthAPI";
$.ajax(uri, {
type: "POST",
data: JSON.stringify(user),
contentType: "application/json",
success: function () {
//here reload the page so that the new vacancy will be showed
alert("logged in");
}
});
I get this error in my chrome console...
No 'Access-Control-Allow-Origin' header is present on the requested resource,
I went through this, and this, and got to know that I have to set Access-Control-Allow-Origin header on the server side to * or set it to my domain.
Now, the actual question is when I hit the same authentication service using fidler everything seems to work just fine, this is driving me crazy guys, please enlighten me
EDIT: And somehow this seems to work with .NET MVC Code too...
public UserData AuthenticateUser(User userDetails)
{
try
{
using (var _client = new HttpClient())
{
var result = new UserData();
_client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
string subUri = Utilities.GetConfigValueFromSection(ConfigurationConstants.AuthenticationServiceSection, ConfigurationConstants.Authenticate);
var postTask = _client.PostAsJsonAsync(_uri + subUri, userDetails).ContinueWith((taskWithResponse) =>
{
var response = taskWithResponse.Result;
var readTask = response.Content.ReadAsAsync<UserData>();
readTask.Wait();
result = readTask.Result;
});
postTask.Wait();
return result;
}
}