Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript &引用;请求的资源不支持http方法';邮政';-405答复_Javascript_Asp.net_Angularjs_Ajax_Asp.net Web Api2 - Fatal编程技术网

Javascript &引用;请求的资源不支持http方法';邮政';-405答复

Javascript &引用;请求的资源不支持http方法';邮政';-405答复,javascript,asp.net,angularjs,ajax,asp.net-web-api2,Javascript,Asp.net,Angularjs,Ajax,Asp.net Web Api2,$.http请求和web api都在本地主机上,但应用程序不同 return $http({ method: "POST", url: config.APIURL + 'Parts', data: {'final':'final'}, headers: { 'Content-Type': 'application/json' } }); angular js(在其他asp.net应用程序中) return $http(

$.http请求和web api都在本地主机上,但应用程序不同

 return $http({
   method: "POST",                       
   url: config.APIURL + 'Parts',
   data: {'final':'final'},
   headers: { 'Content-Type': 'application/json' }
 });
angular js(在其他asp.net应用程序中)

 return $http({
   method: "POST",                       
   url: config.APIURL + 'Parts',
   data: {'final':'final'},
   headers: { 'Content-Type': 'application/json' }
 });
web api(在单独的应用程序中)

 return $http({
   method: "POST",                       
   url: config.APIURL + 'Parts',
   data: {'final':'final'},
   headers: { 'Content-Type': 'application/json' }
 });
错误响应:

 return $http({
   method: "POST",                       
   url: config.APIURL + 'Parts',
   data: {'final':'final'},
   headers: { 'Content-Type': 'application/json' }
 });
{“Message”:“请求的资源不支持http方法 “POST.”

 return $http({
   method: "POST",                       
   url: config.APIURL + 'Parts',
   data: {'final':'final'},
   headers: { 'Content-Type': 'application/json' }
 });
web api 2-已用[HTTPPOST]标记,尽管不需要

 return $http({
   method: "POST",                       
   url: config.APIURL + 'Parts',
   data: {'final':'final'},
   headers: { 'Content-Type': 'application/json' }
 });
我的请求和响应包如下:

 return $http({
   method: "POST",                       
   url: config.APIURL + 'Parts',
   data: {'final':'final'},
   headers: { 'Content-Type': 'application/json' }
 });
**General**
    Request URL:http://localhost/SigmaNest.WebAPI/api/Parts
    Request Method:POST
    Status Code:405 Method Not Allowed
    Remote Address:[::1]:80
    **Response Headers**
    view source
    Allow:GET
    Cache-Control:no-cache
    Content-Length:73
    Content-Type:application/json; charset=utf-8
    Date:Tue, 10 Jan 2017 13:05:59 GMT
    Expires:-1
    Pragma:no-cache
    Server:Microsoft-IIS/10.0
    X-AspNet-Version:4.0.30319
    X-Powered-By:ASP.NET
    **Request Headers**
    view source
    Accept:application/json, text/plain, */*
    Accept-Encoding:gzip, deflate, br
    Accept-Language:en-US,en;q=0.8
    Connection:keep-alive
    Content-Length:17
    Content-Type:application/json;charset=UTF-8
    Host:localhost
    Origin:http://localhost
    Referer:http://localhost/SigmaNest.Web/app/views/index.html
    User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
    **Request Payload**
    view source
    {final: "final"}
    final
    :
    "final"

任何人都可以帮我解决这个405错误。

ASP.Net正在努力将您的Ajax帖子与适当的控制器操作相匹配,因为没有一个与您尝试调用的内容相匹配

 return $http({
   method: "POST",                       
   url: config.APIURL + 'Parts',
   data: {'final':'final'},
   headers: { 'Content-Type': 'application/json' }
 });
在本例中,您试图传递一个对象
{'final':'final'}
,但接受一个字符串
Post(字符串final)
和ASP.Net无法将此与启用了
Post
的任何特定操作相匹配

 return $http({
   method: "POST",                       
   url: config.APIURL + 'Parts',
   data: {'final':'final'},
   headers: { 'Content-Type': 'application/json' }
 });
您可以将javascript对象字符串化

 return $http({
   method: "POST",                       
   url: config.APIURL + 'Parts',
   data: {'final':'final'},
   headers: { 'Content-Type': 'application/json' }
 });
return $http({
   method: "POST",                       
   url: config.APIURL + 'Parts',
   data: JSON.stringify({'final':'final'}), // Strinify your object
   headers: { 'Content-Type': 'application/json' }
 });
或者,更改服务器端方法以接收与所提供对象匹配的类。例如:

 return $http({
   method: "POST",                       
   url: config.APIURL + 'Parts',
   data: {'final':'final'},
   headers: { 'Content-Type': 'application/json' }
 });
// DTO MyObject - .Net will ModelBind your javascript object to this when you post
public class MyObject{
  public string final {get;set;}
}
// change string here to your DTO MyObject
public Part Post(MyObject final){
      ...
}

请把服务器端的代码传递给我们,谢谢。它有效。:)谢谢你的解释,不客气。解释也很重要:)这么多人只是发布代码