我使用jQuery
並使用POST
方法發送數據。但在服務器方法中,值不會到來。什麼可能是錯誤?ASP.NET Core 2,jQuery POST數據null
客戶
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "./AddTag",
dataType: "json",
data: "{'parentId':42,'tagName':'isTagName'}",
success: function (response) {
// ...
}
});
服務器
[HttpPost]
public JObject AddTag(int parentId, string tagName)
{
dynamic answer = new JObject();
List<LogRecord> logs = new List<LogRecord>();
answer.added = fStorage.Tags.AddTag(parentId, tagName, logs);
return answer;
}
固定 非常感謝大家。我明白我的錯誤。我定了這個客戶端和服務器代碼:
let tag = {
"Id": 0,
"ParentId": 42,
"Name": isTagName
};
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "./AddTag",
dataType: "json",
data: JSON.stringify(tag),
success: function (response) {
// ...
}
});
服務器
[HttpPost]
public JObject AddTag([FromBody] Tag tag)
{
dynamic answer = new JObject();
List<LogRecord> logs = new List<LogRecord>();
answer.added = fStorage.Tags.AddTag(tag.ParentId, tag.Name, logs);
answer.logs = Json(logs);
return answer;
}
類增加了
public class Tag
{
public int Id { get; set; }
public int ParentId { get; set; }
public string Name { get; set; }
public List<Tag> ChildsList { get; set; }
[NonSerialized]
public Tag ParrentTag;
}
你發送JSON,而不是查詢字符串,改變你的'data'是 「的parentId = 42&標籤名= isTagName」 和'dataType'到'pplication/X WWW的形式 - urlencoded「並重試。 –
本示例在ASP.NET MVC中正確工作。由服務器處理POST方法有什麼區別? –
「JSON」與兩個「字符串」是不同的類型,所以值得一試。 –