【发布时间】:2017-08-29 11:46:34
【问题描述】:
当我尝试使用 jQuery Ajax 调用 WebAPI 时,我可以知道为什么下面的代码返回 NetworkError 的原因吗? Web方法调用成功,返回后报错。
如果我更改对 HttpGet 的访问权限,我可以使用 IE 访问 Web Method。
所以 jQuery Ajax 一定有问题。希望有人能帮忙。
$.ajax({
type: "POST",
async: false,
url: "http://localhost:5000/API/Test",
xhrFields: { withCredentials: true },
data: JSON.stringify(Params),
contentType: "application/x-www-form-urlencoded",
success: function (msg) {
},
error: function (XHR, errStatus, errorThrown) {
});
[Route("API/Test"), HttpPost]
public string Test()
{
JsonConvert.SerializeObject(new { Test = "Test Message" });
}
【问题讨论】:
-
失败时
errStatus和errorThrown变量的值是多少? -
1) 如果您将一些数据发布到该方法,该方法应该在 [FromBody] 参数中接收它。 2)该方法不返回任何东西。 3) 如果你传递 JSON 数据给它,内容类型应该是 application/json