Web Api Ajax Post调用抛出Http 405错误 [英] Web Api Ajax Post call throws Http 405 error

查看:121
本文介绍了Web Api Ajax Post调用抛出Http 405错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用Asp.net web api Post方法,其中有一个参数名为string类型的值。



代码如下:



在ValueController中:



  public  IHttpActionResult Post(字符串值)
{
return 已创建(Request.RequestUri + / + id = 5,value);
}





 Jquery Calling Web Api:





 $。ajax({
url:' http:// localhost:63022 / api / values'
type:' POST'
data:' {value: ravi}'
contentType: application / json; charset = utf-8
crossDomain: true
成功:函数(数据){
alert(' success');
},
错误:function(x,y,z){
alert(' error');
}
});





问题1:

当我运行此代码并使用上面的jquery ajax调用调用web api Post方法时,我得不到Http错误405方法。



问题2:

修改 ValueController中,添加FromBody属性





  public  IHttpActionResult Post( [FromBody]   string   value 
{
return 已创建(Request.RequestUri + / + id = 5,< span class =code-sdkkeyword> value );
}







当我运行此代码并调用web api Post时使用上面的jquery ajax调用的方法,我在字符串值操作方法参数[Post([FromBody] 字符串值 )]中得到null。



是否必须为Post方法编写FromBody属性才能在问题1中工作?



请问帮助我解决问题1和2 ...

解决方案

.ajax({
url:' http:// localhost:63022 / api / values'
类型:' POST'
data:' { value:ravi}'
contentType: application / json; charset = utf-8
crossDomain: true
成功:函数(数据){
alert(< span class =code-string>' success');
} ,
错误:function(x,y,z){
alert(' error'< /跨度>);
}
});





问题1:

当我运行此代码并使用上面的jquery ajax调用调用web api Post方法时,我得不到Http错误405方法。



问题2:

修改 ValueController中,添加FromBody属性





  public  IHttpActionResult Post( [FromBody]   string   value 
{
return 已创建(Request.RequestUri + / + id = 5,< span class =code-sdkkeyword> value );
}







当我运行此代码并调用web api Post时使用上面的jquery ajax调用的方法,我在字符串值操作方法参数[Post([FromBody] 字符串值 )]中得到null。



是否必须为Post方法编写FromBody属性才能在问题1中工作?



请问帮助我解决问题1和2 ...


这就是我ususaly写我的网页api的方式



 [HttpPost] 
[路线( api / home / activity / {activityID} )]
public object getComments( activityID)
{

// 搜索者activityID代码将在这里
// 返回new {status =!hasError? 成功:失败};
}


尝试将[HttpPost]属性添加到您的函数中

I am calling Asp.net web api Post methos which has one parameter named value of type string.

Code as below:

In ValueController:


public IHttpActionResult Post(string value)
      {
          return Created(Request.RequestUri + "/" + "id=5", value);
      }



Jquery Calling Web Api:



$.ajax({
                    url: 'http://localhost:63022/api/values',
                    type: 'POST',
                    data: '{"value":"ravi"}',
                    contentType: "application/json;charset=utf-8",
                    crossDomain : true,
                    success: function (data) {
                        alert('success');
                    },
                    error: function (x, y, z) {
                        alert('error');
                    }
                });



Question 1:
When I run this code and call web api Post method using above jquery ajax call, I get Http error 405 method not allowed.

Question 2:

Modification in ValueController, adding FromBody attribute



public IHttpActionResult Post([FromBody]string value)
       {
           return Created(Request.RequestUri + "/" + "id=5", value);
       }




When I run this code and call web api Post method using above jquery ajax call, I get null in string value action method parameter [Post([FromBody]string value)].

Is it mandatory to write FromBody attribute for Post method to work in Question 1?

Guys please help me out for question 1 and 2...

解决方案

.ajax({ url: 'http://localhost:63022/api/values', type: 'POST', data: '{"value":"ravi"}', contentType: "application/json;charset=utf-8", crossDomain : true, success: function (data) { alert('success'); }, error: function (x, y, z) { alert('error'); } });



Question 1:
When I run this code and call web api Post method using above jquery ajax call, I get Http error 405 method not allowed.

Question 2:

Modification in ValueController, adding FromBody attribute



public IHttpActionResult Post([FromBody]string value)
       {
           return Created(Request.RequestUri + "/" + "id=5", value);
       }




When I run this code and call web api Post method using above jquery ajax call, I get null in string value action method parameter [Post([FromBody]string value)].

Is it mandatory to write FromBody attribute for Post method to work in Question 1?

Guys please help me out for question 1 and 2...


This is how I ususaly write my web api

[HttpPost]
[Route("api/home/activity/{activityID}")]
public object getComments(long activityID)
{

          //Search by activityID code will be here
       // return new { status = !hasError ? "Success" : "Fail" };
}


Try adding the [HttpPost] attribute to your function.


这篇关于Web Api Ajax Post调用抛出Http 405错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆