1

Going crazy. The first call sets the session and the 2nd call gets the session. First 'Post' call return username and session id correctly but when I try to get the session, it returns blank with status code 200.

The same is the case with HttpClient (C#) code.

Both the call works perfectly if I try to set through browser or PostMan.

        $.ajax({
        url: "http://localhost:xxxx/xxxx/api/v1/session?username=xxxx&password=xxxx", 
        type: 'POST',            
        dataType: 'application/json',
        success: function (result) {
            $.ajax({
                url: "http://localhost:xxxx/xxxx/api/v1/session",
                type: 'Get',
                dataType: 'application/json',
                crossDomain: true,
                success: function (result) {
                    debugger;

                },
                error: function (result) {
                    debugger;
                }
            });
        },
        error: function (result) {
            debugger;
            $.ajax({
                url: "http://localhost:xxxx/xxxx/api/v1/session",
                type: 'Get',
                dataType: 'application/json',
                crossDomain: true,

                success: function (result) {
                    debugger

                },
                error: function (result) {
                    debugger;
                }
            });
        }
    });

The Get Request from Post Man {"sessionId":"088BE3296B8778948F649A6B7B8C064B","userName":"user1"}

Am I missing anything?

Do note that Web-APIs are exposed by third party.

1
  • Status 200 is OK. So can you change the success of the 2nd request to another name, like "resultSession". Tell me the result. Commented Aug 8, 2018 at 7:06

2 Answers 2

4

The problem is you have used the post method and trying to pass the data in query string.

function DoLogin() {
        if (document.getElementById("Email").value == "" && document.getElementById("Password").value == "") {
            document.getElementById("Email").focus();
            toastr.error("Please enter email and password.");
        }
        else if (document.getElementById("Email").value == "") {
            document.getElementById("Email").focus();
            toastr.error("Please enter valid email address.");
        }
        else if (document.getElementById("Password").value == "") {
            document.getElementById("Password").focus();
            toastr.error("Please enter valid password.");
        }
        else {
            document.getElementById("ButtonLogin").value = "Validating your account...";
            document.getElementById("ButtonLogin").disabled = true;
            var model = {
                Email: $('#Email').val(),
                Password: $('#Password').val()
            }
            $.ajax({
                type: "POST",
                data: JSON.stringify(model),
                url: "@Url.Action("Login", "Account", null)",
                contentType: "application/json"
            }).success(function (res) {
                var Type = res.type;
                if (Type == "error") {
                    toastr.error(res.value);
                    document.getElementById("ButtonLogin").value = "login";
                    document.getElementById("ButtonLogin").disabled = false;
                }
                else {
                    window.location.href = res.returnUrl;
                }
            });
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Tried but it throws bad request error. Ideally it should work but no luck. As I mentioned I tried with HttpClient (C#) method too.
1

Usually, in the response for the POST request, there will be a Set-Cookie header.

You may need to pass the cookie in the GET request.

2 Comments

How come its working in POSTMan / directly through browser then? And what would be the cookie value?
@Abhishek at least when in browser, it will automatically attach cookie in the request. That's why I guess it might be cookie. You can check this using Network panel in Chrome Developer tool to see the difference between successful request and failed request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.