简体   繁体   English

Angular2 / 4中的405(不允许使用方法)

[英]405 (Method not allowed) in angular2/4

updateUserDetails(ename,ejobtitle,edept,eunit,equal,eaqser,empid)
{

  let url = GlobalVariable.BASE_API_URL + "api/updateUserProfile";    
  let headers = new Headers({'Content-Type':'application/json'});
  let options = new RequestOptions({headers:headers});
  const body = 
  {"empno":empid,"name":ename,"jobtitle":ejobtitle,"dept":edept,"unit":eunit,"qualification":equal,"aqinservice":eaqser}

  return this.http.post(url,body,options)
  .map(resp=>resp.json());    

}

API API

[Route("api/updateUserProfile")]
    [HttpPost]       
    public HttpResponseMessage updateUserProfile(HttpResponseMessage Req)
    {
        try
        {
            string postData = new System.IO.StreamReader(HttpContext.Current.Request.InputStream).ReadToEnd();

            UserMaster.Instance.UpdateUserProfile(postData);
            var JSONString = new StringBuilder();
            JSONString.Append("{");
            JSONString.Append("\"status" + "\":" + "\"ok" + "\",");
            JSONString.Append("\"message" + "\":" + "\"Record Updated successfilly." + "\"");              
            JSONString.Append("}");
            var response = Request.CreateResponse(HttpStatusCode.OK);
            response.Content = new StringContent(JSONString.ToString(), System.Text.Encoding.UTF8, "application/json");
            return response;

        }
}

I am getting error in angular 4 - 405 (Method Not allowed) 我在角度4-405中遇到错误(不允许使用方法)

But it works perfectly in Fiddler. 但是它在Fiddler中完美地工作。

There exist a lot of ways to do this, one of it is you can use Object.keys() and Object.values() method to extract keys and values from your data. 有很多方法可以做到这一点,其中之一是您可以使用Object.keys()和Object.values()方法从数据中提取键和值。

      keys:Array<String>;
      values:Array<String>;
    item={"em id":"101","name":"ABC","role":"admin"};

 myFun()
    {
     this.keys=Object.keys(this.item);
      this.values=Object.values(this.item);
    }

The most simplest way to use that object is to just give it a name and access its properties. 使用该对象的最简单方法是为其命名并访问其属性。

example , 例如

item = {"em id":"101","name":"ABC","role":"admin"}; 

and then using it like this , 然后像这样使用它

console.log("em_id is " + item.em_id + "name is " + item.name + "role is" + item.role );

But if you really want to store it in variable then just do it in same way , 但是,如果您真的想将其存储在变量中,则只需以相同的方式进行操作,

let em_id = item.em_id;
let name = item.name;
let role = item.role;

The problem is the " preflight request ". 问题是“ 飞行前请求 ”。 Adding the OPTIONS method to your api and then just return should work. 将OPTIONS方法添加到您的api,然后返回就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM