我有一个简单的ASP.NET Core API,可使用javascript的fetch方法进行调用。但是类型为[[SearchString属性的输入变量model始终为null。
当我使用Postman调用它时,它按预期方式工作,但由于某种原因,它不是从javascript调用。API
:[HttpPost]
public async Task<IActionResult> Search([FromBody] BasicSearchModel model)
{
....
}
:BasicSearchModel.cs
public interface IBasicSearchModel
{
string SearchString { get; set; }
}
public class BasicSearchModel : IBasicSearchModel
{
public string SearchString { get; set; }
}
:JavaScript
const WebRequest = async (controller = "API", action, query = {}, data = {}, method = RequestMethods.POST, mode = RequestModes.SameOrigin, creds = RequestCredentials.SameOrigin, headers = {
"Content-Type": "application/json",
"Accept": "application/json"
}) => {
const GetUrl = () => {
var result = "/" + controller + "/" + action;
var i = 0;
for (var q in query) {
if (i === 0) {
result += "?";
} else {
result += "&";
}
result += q[0] + "=" + q[1];
i++;
}
return result;
}
await fetch(GetUrl(), {
method: method,
headers: headers,
// @ts-ignore
credentials: creds.toLowerCase(),
// @ts-ignore
body: data,
// @ts-ignore
mode: mode.toLowerCase(),
})
.then(response => {
if (response.ok) {
return new RequestResult(response);
} else {
throw new Error("Request failed: " + response.status + "; " + response.statusText);
}
})
.catch(error => {
throw new Error("Error: " + error.statusText);
});
};
:JavaScript调用
var result = await WebRequest("Identity", "Search", null, { SearchString: str });
:[Request
标题
:接受:“ application / json”,“ Content-Type”:“ application / json”方法
:“ POST”Body
:SearchString:“测试人员”我有一个简单的ASP.NET Core API,可使用javascript的fetch方法进行调用。但是SearchString属性类型的输入变量模型始终为null。当我使用...