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

Posted

技术标签:

【中文标题】通过 C# 或 jQuery ajax 调用时,Web API 不起作用【英文标题】:Web API is not working when calling through C# or jQuery ajax 【发布时间】:2019-01-15 08:16:12 【问题描述】:

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

【问题讨论】:

状态 200 正常。那么您能否将第二个请求的成功更改为另一个名称,例如“resultSession”。告诉我结果。 【参考方案1】:

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

【讨论】:

试过了,但它抛出了错误的请求错误。理想情况下它应该工作,但没有运气。正如我所提到的,我也尝试过使用 HttpClient (C#) 方法。【参考方案2】:

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

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

【讨论】:

那么它是如何在 POSTMan / 直接通过浏览器工作的呢? cookie 的值是多少? @Abhishek 至少在浏览器中,它会自动在请求中附加cookie。这就是为什么我猜它可能是cookie。您可以使用 Chrome 开发者工具中的网络面板来检查这一点,以查看成功请求和失败请求之间的区别。

以上是关于通过 C# 或 jQuery ajax 调用时,Web API 不起作用的主要内容,如果未能解决你的问题,请参考以下文章

MVC 3 / Jquery AJAX / Session Expires / C# - 在 ajax 调用期间处理会话超时

通过 Jquery 使用 Ajax 调用函数/数据库更新

JQuery ajax 调用 httpget webmethod (c#) 不工作

jQuery调用已被ajax加载或通过html()方法插入的div元素

jquery ajax'post'调用

为啥这个对 C# 方法的 jquery ajax GET 调用不起作用?