C# 通过C或jQuery ajax调用时,Web API不起作用
快疯了。第一个调用设置会话,第二个调用获取会话。第一次“Post”调用返回用户名和会话id正确,但当我尝试获取会话时,它返回空白,状态代码为200 httpc客户端代码也是如此 如果我尝试通过浏览器或邮递员进行设置,这两个呼叫都能完美工作C# 通过C或jQuery ajax调用时,Web API不起作用,c#,jquery,ajax,api,asp.net-web-api,C#,Jquery,Ajax,Api,Asp.net Web Api,快疯了。第一个调用设置会话,第二个调用获取会话。第一次“Post”调用返回用户名和会话id正确,但当我尝试获取会话时,它返回空白,状态代码为200 httpc客户端代码也是如此 如果我尝试通过浏览器或邮递员进行设置,这两个呼叫都能完美工作 $.ajax({ url: "http://localhost:xxxx/xxxx/api/v1/session?username=xxxx&password=xxxx", type: 'POST',
$.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请求
{会话ID:088BE3296B8778948F649A6B7B8C064B,用户名:user1}
我遗漏了什么吗
请注意,Web API是由第三方公开的。通常,在POST请求的响应中,会有一个设置的Cookie头
您可能需要在GET请求中传递cookie。通常,在POST请求的响应中,会有一个设置的cookie头
您可能需要在GET请求中传递cookie。问题是您使用了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方法并试图以查询字符串形式传递数据
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;
}
});
}
}
状态200正常。因此,您可以将第二个请求的成功更改为另一个名称,如resultSession。告诉我结果。状态200正常。因此,您可以将第二个请求的成功更改为另一个名称,如resultSession。告诉我结果。为什么它在邮递员/直接通过浏览器工作?cookie值是多少?@Abhishek至少在浏览器中,它会自动将cookie附加到请求中。这就是为什么我猜可能是饼干。您可以使用Chrome Developer工具中的网络面板来检查这一点,以查看成功请求和失败请求之间的区别。那么,它是如何在POSTMan/中直接通过浏览器工作的呢?cookie值是多少?@Abhishek至少在浏览器中,它会自动将cookie附加到请求中。这就是为什么我猜可能是饼干。您可以使用Chrome Developer工具中的网络面板来检查这一点,以查看成功请求和失败请求之间的差异。已尝试,但会抛出错误请求错误。理想情况下,它应该能工作,但没有运气。正如我提到的,我也尝试了httpc客户端方法。尝试了,但它抛出了错误的请求错误。理想情况下,它应该能工作,但没有运气。正如我提到的,我也尝试过使用httpc客户端方法。