1

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!"}
DeepToot
  • 65
  • 13
  • Can you share how JSON.stringify(cred) looks like? – Ozan Gunceler Jul 09 '16 at 23:07
  • @OzanGunceler - I updated my question with this information. Thanks for the time. – DeepToot Jul 09 '16 at 23:10
  • 1
    Did you try removing JSON.stringify? Set data to the cred object directly – Ozan Gunceler Jul 09 '16 at 23:20
  • I did not. I can try that real quick, but I have confirmed switching back my wcf method to return just a string works as expected. Its only when I return the HttpResponseMessage that it fails. Any reason why my incoming data would need to change? – DeepToot Jul 09 '16 at 23:23
  • Interestingly enough, that returned a 500 instead of connection reset..the 500 was due to 'DeserializationFailed' : 'The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'Login'. Encountered unexpected character 'u'.' – DeepToot Jul 09 '16 at 23:25
  • I think your solution is here : http://stackoverflow.com/a/32671585/4848251 – Ozan Gunceler Jul 09 '16 at 23:39
  • @OzanGunceler I appreciate the time on this. I checked out the other page and wasn't able to find anything there that helped. I tried setting my UriTemplate in my web service to '/login/?username={username}&password={password}' and it started giving me the connection reset exception again. I also tried setting my ajax url to mimick web service Uri..removed that data field in ajax..then put it back..put back on the stringify on data then removed it again. Nothing has worked so far. – DeepToot Jul 10 '16 at 03:05

0 Answers0