代码之家  ›  专栏  ›  技术社区  ›  Abhishek Vyas

通过c或jquery ajax调用时,Web API不工作

  •  1
  • Abhishek Vyas  · 技术社区  · 7 年前

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

    httpclient(c)代码也是如此。

    如果我尝试通过浏览器或邮递员进行设置,这两个调用都能很好地工作。

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

    邮递员的GET请求 “sessionid”:“088be3296b8778948f649a6b7b8c064b”,“用户名”:“用户1”

    我错过什么了吗?

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

    2 回复  |  直到 7 年前
        1
  •  1
  •   Alan haha    7 年前

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

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

        2
  •  5
  •   Yudiz    7 年前

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