I have an ajax POST call to my wcf service. It was working fine when I was returning a string (json payload) but as soon as I switched to returning an 'HttpResponseMessage' object it started giving me the ERR_CONNECTION_RESET. Same payload returned with the exception of now I want to return proper responses like 200 and 404's.
Here is the ajax call:
function Login (cred) {
$.ajax({
type: "POST",
url: ServerConfig.Server + ServerConfig.Service + "/login/",
// The key needs to match your method's input parameter (case-sensitive).
data: JSON.stringify(cred),
contentType: "application/json",
dataType: "json",
crossDomain: true,
beforeSend: function(xhr) {
xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
},
error: loginFailed,
success: loginSuccess
});
}
and here is the my wcf method:
public HttpResponseMessage Login(string username, string password)
{
repository = new DbRepository();
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.BadRequest);
var res = string.Empty;
var user = repository.Login(username, password);
if (null != user)
{
res = JsonConvert.SerializeObject(user);
response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(res)
};
}
else
{
return response;
}
return response;
}
I have confirmed that by switching back to string works as expected. Can someone shed some light on what might be going on?
Thanks.
EDIT: Here is what my 'cred' object looks like and how it was created:
$('.signin').on("click", function () {
var user = $('.username').val();
var pass = $('.password').val();
var cred = { "username": user, "password": pass };
Login(cred);
});
The JSON.stringify(cred) looks like this:
{"username":"Test1234","password":"TestPassword!"}