通过C#或jQuery ajax调用时Web API不起作用

时间:2018-08-08 06:58:15

标签: c# jquery ajax api asp.net-web-api

发疯。第一个呼叫设置会话,第二个呼叫获取会话。第一个“发布”呼叫正确返回了用户名和会话ID,但是当我尝试获取会话时,它将返回状态码为200的空白。

HttpClient(C#)代码也是如此。

如果我尝试通过浏览器或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;
                }
            });
        }
    });

邮递员的获取请求 {“ sessionId”:“ 088BE3296B8778948F649A6B7B8C064B”,“ userName”:“ user1”}

我想念什么吗?

请注意,Web-API是由第三方公开的。

2 个答案:

答案 0 :(得分:4)

问题是您已经使用了post方法,并试图在查询字符串中传递数据。

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;
                }
            });
        }
    }

答案 1 :(得分:1)

通常,在POST请求的响应中,将有一个Set-Cookie标头。

您可能需要在GET请求中传递cookie。