繁体   English   中英

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

[英]Web API is not working when calling through C# or jQuery ajax

快要疯了。 第一个呼叫设置会话,第二个呼叫获取会话。 第一次“发布”呼叫返回的用户名和会话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是由第三方公开的。

问题是您使用了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;
                }
            });
        }
    }

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM