Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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
Post Angular 2-web api:无法在api上获取发布的数据_Post_Asp.net Web Api_Angular2 Forms_Angular2 Services - Fatal编程技术网

Post Angular 2-web api:无法在api上获取发布的数据

Post Angular 2-web api:无法在api上获取发布的数据,post,asp.net-web-api,angular2-forms,angular2-services,Post,Asp.net Web Api,Angular2 Forms,Angular2 Services,从我正在发送数据的页面: register() { this.loading = true; this.Register = { firstName: 'aa', lastName: 'aa', email: 'aa', password: 'aa', cpassword: 'aa', gender: "male", dob: '2017-05-02' }; this.userService.create(this.Register)

从我正在发送数据的页面:

     register() {
        this.loading = true;
        this.Register = { firstName: 'aa', lastName: 'aa', email: 'aa', password: 'aa', cpassword: 'aa', gender: "male", dob: '2017-05-02' };
        this.userService.create(this.Register)
            .subscribe(
            data => {
                alert("aa");
            },
            error => {
                // this.alertService.error(error);
                this.loading = false;
            });
    }
我发布了Angular 2服务代码中的数据,如下所示:

    create(UserRegister: UserRegister) {  
       return this.http.post('http://localhost:53625/api/UserLoginAPI/InsertUser', UserRegister, this.jwt()).map((response: Response) => response.json());
    }

    private jwt() {   
            let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'  });
            return new RequestOptions({ headers: headers });
    }
       [HttpPost]
        public IHttpActionResult InsertUser(UserRegisterModel UserRegister)
        {
            int returnval = 0;
            returnval = objUserLoginBAL.InsertUser(objUserRegisterModel);
            return Json(returnval);
        }
为此,我在api中编写的代码如下:

    create(UserRegister: UserRegister) {  
       return this.http.post('http://localhost:53625/api/UserLoginAPI/InsertUser', UserRegister, this.jwt()).map((response: Response) => response.json());
    }

    private jwt() {   
            let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'  });
            return new RequestOptions({ headers: headers });
    }
       [HttpPost]
        public IHttpActionResult InsertUser(UserRegisterModel UserRegister)
        {
            int returnval = 0;
            returnval = objUserLoginBAL.InsertUser(objUserRegisterModel);
            return Json(returnval);
        }
我在api端得到调用,但我的对象没有得到任何值。如图所示:

尝试以下操作:

 create(UserRegister: UserRegister) {  
   return this.http.post('http://localhost:53625/api/UserLoginAPI/InsertUser', UserRegister, this.jwt(UserRegister)).map((response: Response) => response.json());
}

private jwt(UserRegister: UserRegister) {   
let _body = JSON.stringify(UserRegister);
let headers = new Headers({ 'Content-Type': 'application/json;charset=utf-8' });

return new RequestOptions({ 
          headers: headers,
          body: _body });
}
请尝试以下内容:

 create(UserRegister: UserRegister) {  
   return this.http.post('http://localhost:53625/api/UserLoginAPI/InsertUser', UserRegister, this.jwt(UserRegister)).map((response: Response) => response.json());
}

private jwt(UserRegister: UserRegister) {   
let _body = JSON.stringify(UserRegister);
let headers = new Headers({ 'Content-Type': 'application/json;charset=utf-8' });

return new RequestOptions({ 
          headers: headers,
          body: _body });
}

我认为问题在于你的头球构造。尝试更改此选项:

'Content-Type': 'application/x-www-form-urlencoded'
为此:

'Content-Type': 'application/json'

UserRegisterModel中为cpassword引入一个字段,我认为问题在于标题的构造。尝试更改此选项:

'Content-Type': 'application/x-www-form-urlencoded'
为此:

'Content-Type': 'application/json'

另外,在UserRegisterModel中为cpassword引入一个字段,您的初始页面应该是这样的:注意,我删除了
cpassword
,因为它不在您的API模型中

register() {
  this.loading = true;

  this.Register = { 
    dob: '2017-05-02' 
    email: 'aa', 
    firstName: 'aa', 
    gender: "male",
    lastName: 'aa', 
    password: 'aa',  
  };

  this.userService.create(this.Register)
    .subscribe((data:any) => {
        alert("aa");
    },(error:any) => {
        // this.alertService.error(error);
        this.loading = false;
    });
}
您的服务:我清理了
jwt()
函数

import { Http, Response, Headers, RequestOptionsArgs} from '@angular/http';

create(userRegister: UserRegister) {  
  let url:string = 'http://localhost:53625/api/UserLoginAPI/InsertUser';

  return this.http.post(url, userRegister, this.jwt())
    .map((res:Response) => res.json());
}

private jwt():RequestOptionsArgs {   
  let headers:Headers = new headers();

  headers.append('Content-Type', 'application/json');

  let options:RequestOptionsArgs = new RequestOptions();

  options.headers = headers;

  return options;
}

您的初始页面应该是这样的:注意我删除了
cpassword
,因为它不在您的API模型中

register() {
  this.loading = true;

  this.Register = { 
    dob: '2017-05-02' 
    email: 'aa', 
    firstName: 'aa', 
    gender: "male",
    lastName: 'aa', 
    password: 'aa',  
  };

  this.userService.create(this.Register)
    .subscribe((data:any) => {
        alert("aa");
    },(error:any) => {
        // this.alertService.error(error);
        this.loading = false;
    });
}
您的服务:我清理了
jwt()
函数

import { Http, Response, Headers, RequestOptionsArgs} from '@angular/http';

create(userRegister: UserRegister) {  
  let url:string = 'http://localhost:53625/api/UserLoginAPI/InsertUser';

  return this.http.post(url, userRegister, this.jwt())
    .map((res:Response) => res.json());
}

private jwt():RequestOptionsArgs {   
  let headers:Headers = new headers();

  headers.append('Content-Type', 'application/json');

  let options:RequestOptionsArgs = new RequestOptions();

  options.headers = headers;

  return options;
}

你能分享你的代码片段吗?你是如何用值调用create方法的?你能尝试在headers的request options中添加body对象并尝试吗?我不明白,请给我一个例子我得到了错误,比如requestOptions的arguement是不可赋值的参数类型字符串| RequestHave以你的例子更新了我的答案,试一试。你能分享你的代码片段吗?你是如何用值调用create方法的?你能尝试在标题的请求选项中添加body对象并尝试吗?我不明白,请给我一个例子我得到了错误,比如请求选项的参数不可赋值字符串的参数类型|请求通过使用你的举个例子,试一试。