【问题标题】:How to send form data to REST api in ionic 3如何在 ionic 3 中将表单数据发送到 REST api
【发布时间】:2019-08-14 03:14:31
【问题描述】:

当我将数据发送到 REST API 时,它会以单个数组的形式发送,例如

Array ([  
   {  
      "username":"karthik@w3cert_in",
      "password":"05550",
      "grant_type":"password",
      "client_id":"Outfit1548925669",
      "client_secret":"a10620c85033ab02b582d17716cda245"
   }
]=> )

但我需要发送这样的数据

Array
(
    [username] => karthik@w3cert.in
    [password] => 05550
    [grant_type] => password
    [client_id] => Outfit1548925669
    [client_secret] => a10620c85033ab02b582d17716cda245
)

这是我正在使用的代码:

 let apiUrl = this.urlService.apiUrl  + 'oauth/access_token';
    let headers = new Headers({'Content-Type' : 'application/x-www-form-urlencoded'});
    let options = new RequestOptions({
            headers: headers});


  let postcredentials = {
      'username' : credentials.username,
      'password' : credentials.password,
      'grant_type' : credentials.grant_type,
      'client_id' : credentials.client_id,
      'client_secret' : credentials.client_secret,
  };

    console.log('iii'+(postcredentials) );
       this.http.post(apiUrl, postcredentials,  options)
         .subscribe(res => {
           console.log(JSON.parse(JSON.stringify(res)));
         }, (err) => {
           console.log(apiUrl);
         });
  }

【问题讨论】:

    标签: arrays laravel typescript ionic-framework ionic3


    【解决方案1】:

    而不是像这样将数据存储到对象中。

     let   postcredentials = [];
       for (var prop in credentials) {
            postcredentials[prop]= credentials.prop
       }
    

    【讨论】:

    • 你能解释一下你提到的道具是什么@pal
    • prop 是对象 @Karthik 的关键
    • 如果我们这样使用,它会在 prop 上显示为错误... let postcredentials = []; for (var prop in credentials) { postcredentials[username]: credentials[username], }
    • 请检查编辑后的答案应该是“=”而不是“:”
    【解决方案2】:

    这是因为 Laravel 解析您的请求并将所有请求参数(POST、GET、PUT、DELETE)收集到一个数组中,这就是您在此处看到的:

    Array ([  
       {  
          "username":"karthik@w3cert_in",
          "password":"05550",
          "grant_type":"password",
          "client_id":"Outfit1548925669",
          "client_secret":"a10620c85033ab02b582d17716cda245"
       }
    ]=> )
    

    此外,每个请求参数都是一个键值对组合,因此,每个请求值都必须有一个用于访问它的键,这意味着您必须为下面的数据组分配一个键才能访问它作为单个数组。

    let postcredentials = {
          'username' : credentials.username,
          'password' : credentials.password,
          'grant_type' : credentials.grant_type,
          'client_id' : credentials.client_id,
          'client_secret' : credentials.client_secret,
    };
    

    类似这样的:

    let postcredentials = {
          'auth_data': {
              'username' : credentials.username,
              'password' : credentials.password,
              'grant_type' : credentials.grant_type,
              'client_id' : credentials.client_id,
              'client_secret' : credentials.client_secret,
           }
     };
    

    然后在你的 Laravel 控制器中,你可以像这样获取数据:

    public function loginFn(Request $request){
        print_r($request->auth_data);
    }
    

    查看:

    Array
    (
        [username] => karthik@w3cert.in
        [password] => 05550
        [grant_type] => password
        [client_id] => Outfit1548925669
        [client_secret] => a10620c85033ab02b582d17716cda245
    )
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2018-07-24
      • 2017-08-08
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多