【发布时间】:2017-11-24 12:23:38
【问题描述】:
所以,前段时间,我需要发出 ajax 请求来访问后面的代码。我做到了 (here)。
但现在我正在使用 asp.NET Core 2.0,它的工作方式有所不同。好吧,现在我可以提出请求,但由于某种原因,变量为空(仅在后面的代码中,在客户端它们有内容。可能没什么大不了的,但我不明白为什么。
所以,这是我的 ajax 函数:
function Ajax(expression1, expression2, url, json, error) {
console.log(expression1+" - "+ expression2);
var request = { email: expression1, password: expression2 }
$.ajax({
url: url,
method: 'post',
contentType: 'application/json',
data: JSON.stringify(request),
dataType: 'json',
success: function (resp) {
console.log(request);
if (expression1 === null || expression2===null)
window.location.href = resp[json];
else
document.getElementById(error).innerHTML = resp[json];
},
error: function (error) {
}
})
}
还有c#代码:
public string ExternalLogin(string email, string name)
{
//TODO: Facebook login
// Redirect link example. Could be another one.
return "{\"facebook\":\"Home/About\"}";
}
ajax 函数发出请求并触发 c# 函数,并返回我要求的内容。唯一的问题是空变量。为什么会这样?
编辑:请求的用户类:
public class User
{
[JsonProperty(PropertyName ="email")]
public string Email { get; set; }
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "password")]
public string Password { get; set; }
}
【问题讨论】:
标签: c# ajax asp.net-core-2.0