尽管启用了 CORS,但无法使用 ajax post 到另一个域
Cannot post to another domain using ajax despite having CORS enabled
我正在使用 MVC.net 并将 post 数据发送到我的 Web API 2 控制器。我不断收到 500 内部服务器错误消息。
我正在尝试 post 到另一个域,如果这很重要?我有 2 个 visual studio 个实例 运行,一个充当客户端,另一个充当服务器。我启用了 CORS。
GET 在此设置下运行良好,但现在我正在尝试 post。
我在 'server' 上的控制器是
[HttpPost]
[Route("api/cms/")]
public IHttpActionResult Post([FromBody]int accountId, [FromBody]string content, [FromBody]string paneId, [FromBody]string url)
{
//content
}
我使用的 'client' 上的 javascript 是
function ajaxStart(type, url, data, successDelegate, failDelegate, errorDelegate) {
$.ajax({
type: type.toUpperCase(),
url: url,
contentType: "application/json;charset=utf-8",
data: data,
dataType: "json",
success: function (response) {
successDelegate(response);
},
failure: function (e) {
failDelegate(e.statusText);
},
error: function (e) {
errorDelegate(e.statusText); //always hit
}
})
}
数据是用(我故意使用无意义的字符串只是为了确保格式没有问题)创建的
var data = JSON.stringify({ accountId: 1, paneId: "_selectedPaneNumber", url: "_url", content: "content" });
并且 Google Chrome 开发工具中的 'header' 视图显示:
我不知道我做错了什么。
如果您想将字符串作为 body 发送,请执行以下操作:
- 添加header:
Content-Type: application/x-www-form-urlencoded
- 更改 body 中的字符串值,使其以
=
字符为前缀:=5
客户端的 javascript 显示正常。问题似乎出在 ApiController 的操作和参数绑定上。
At most one parameter is allowed to read from the message body. So this will not work:
public IHttpActionResult Post([FromBody]int accountId, [FromBody]string content, [FromBody]string paneId, [FromBody]string url) { ... }
The reason for this rule is that the request body might be stored in a non-buffered stream that can only be read once.
来源:Parameter Binding in ASP.NET Web API : Using [FromBody]
考虑在动作服务器端创建模型
public class MyModel {
public int accountId { get; set; }
public string content { get; set; }
public string paneId { get; set; }
public string url { get; set; }
}
并更新预期的操作。
[HttpPost]
[Route("api/cms/")]
public IHttpActionResult Post([FromBody] MyModel model) {
//code removed for brevity
}
我正在使用 MVC.net 并将 post 数据发送到我的 Web API 2 控制器。我不断收到 500 内部服务器错误消息。
我正在尝试 post 到另一个域,如果这很重要?我有 2 个 visual studio 个实例 运行,一个充当客户端,另一个充当服务器。我启用了 CORS。
GET 在此设置下运行良好,但现在我正在尝试 post。
我在 'server' 上的控制器是
[HttpPost]
[Route("api/cms/")]
public IHttpActionResult Post([FromBody]int accountId, [FromBody]string content, [FromBody]string paneId, [FromBody]string url)
{
//content
}
我使用的 'client' 上的 javascript 是
function ajaxStart(type, url, data, successDelegate, failDelegate, errorDelegate) {
$.ajax({
type: type.toUpperCase(),
url: url,
contentType: "application/json;charset=utf-8",
data: data,
dataType: "json",
success: function (response) {
successDelegate(response);
},
failure: function (e) {
failDelegate(e.statusText);
},
error: function (e) {
errorDelegate(e.statusText); //always hit
}
})
}
数据是用(我故意使用无意义的字符串只是为了确保格式没有问题)创建的
var data = JSON.stringify({ accountId: 1, paneId: "_selectedPaneNumber", url: "_url", content: "content" });
并且 Google Chrome 开发工具中的 'header' 视图显示:
我不知道我做错了什么。
如果您想将字符串作为 body 发送,请执行以下操作:
- 添加header:
Content-Type: application/x-www-form-urlencoded
- 更改 body 中的字符串值,使其以
=
字符为前缀:=5
客户端的 javascript 显示正常。问题似乎出在 ApiController 的操作和参数绑定上。
At most one parameter is allowed to read from the message body. So this will not work:
public IHttpActionResult Post([FromBody]int accountId, [FromBody]string content, [FromBody]string paneId, [FromBody]string url) { ... }
The reason for this rule is that the request body might be stored in a non-buffered stream that can only be read once.
来源:Parameter Binding in ASP.NET Web API : Using [FromBody]
考虑在动作服务器端创建模型
public class MyModel {
public int accountId { get; set; }
public string content { get; set; }
public string paneId { get; set; }
public string url { get; set; }
}
并更新预期的操作。
[HttpPost]
[Route("api/cms/")]
public IHttpActionResult Post([FromBody] MyModel model) {
//code removed for brevity
}