javascript – WebApi 2 POST,单字符串参数不起作用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – WebApi 2 POST,单字符串参数不起作用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2628字,纯文字阅读大概需要4分钟。
内容图文
![javascript – WebApi 2 POST,单字符串参数不起作用](/upload/InfoBanner/zyjiaocheng/706/9791c93512c2403f8d69e4d52081b0e2.jpg)
我有以下控制器:
public class ValuesController : ApiController
{
// POST api/values
public IHttpActionResult Post(string filterName)
{
return new JsonResult<string>(filterName, new JsonSerializerSettings(), Encoding.UTF8, this);
}
}
WebApi配置
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional });
我使用这个js代码来调用api
$.ajax(
{
url: "/api/values/",
type: "POST",
dataType: 'json',
data: { filterName: "Dirty Deeds" },
success: function (result) {
console.log(result);
},
error: function (xhr, status, p3, p4) {
var err = "Error " + " " + status + " " + p3;
if (xhr.responseText && xhr.responseText[0] == "{")
err = JSON.parse(xhr.responseText).message;
console.log(err);
}
});
我得到405方法不允许(帖子)
解决方法:
C#
public class ValuesController : ApiController
{
// POST api/values
[HttpPost] // added attribute
public IHttpActionResult Post([FromBody] string filterName) // added FromBody as this is how you are sending the data
{
return new JsonResult<string>(filterName, new JsonSerializerSettings(), Encoding.UTF8, this);
}
JavaScript的
$.ajax(
{
url: "/api/Values/", // be consistent and case the route the same as the ApiController
type: "POST",
dataType: 'json',
data: "=Dirty Deeds", // add an = sign
success: function (result) {
console.log(result);
},
error: function (xhr, status, p3, p4) {
var err = "Error " + " " + status + " " + p3;
if (xhr.responseText && xhr.responseText[0] == "{")
err = JSON.parse(xhr.responseText).message;
console.log(err);
}
});
说明
因为您只发送一个值,所以在它前面添加=符号,因此它将被视为表单编码.如果要明确这是您正在对ajax调用执行的操作,还可以添加内容类型.
contentType: 'application/x-www-form-urlencoded'
或者,您也可以通过URL发送内容或将内容包装在服务器上的对象以及ajax调用中并对其进行字符串化.
public class Filter {
public string FilterName {get;set;}
}
public class ValuesController : ApiController
{
// POST api/values
[HttpPost] // added attribute
public IHttpActionResult Post([FromBody] Filter filter) // added FromBody as this is how you are sending the data
{
return new JsonResult<string>(filter.FilterName, new JsonSerializerSettings(), Encoding.UTF8, this);
}
JavaScript的
$.ajax(
{
url: "/api/Values/", // be consistent and case the route the same as the ApiController
type: "POST",
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify({FilterName: "Dirty Deeds"}), // send as json
success: function (result) {
console.log(result);
},
error: function (xhr, status, p3, p4) {
var err = "Error " + " " + status + " " + p3;
if (xhr.responseText && xhr.responseText[0] == "{")
err = JSON.parse(xhr.responseText).message;
console.log(err);
}
});
内容总结
以上是互联网集市为您收集整理的javascript – WebApi 2 POST,单字符串参数不起作用全部内容,希望文章能够帮你解决javascript – WebApi 2 POST,单字符串参数不起作用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。