【问题标题】:how to post associative array in ionic 3如何在 ionic 3 中发布关联数组
【发布时间】:2019-08-14 05:31:33
【问题描述】:

当我尝试使用 ionic 将关联数组中的数据发布到 rest api 时。 服务器端只接收空数据。 这种方法是否正确或建议任何其他方法

   public login(credentials) {

    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});

     var postcredn=new Array();

     postcredn['username'] = "karthik@abcde.in";
     postcredn['password'] = "05550";
     postcredn['grant_type'] = "password";
     postcredn['client_id'] = "Outfit1548669";
     postcredn['client_secret'] = "a10620c85033abd17716cda245";


    console.log('iii'+postcredn['username'] );

      return new Promise((resolve, reject) => {
          this.http.post(apiUrl, postcredn,  options)

           .subscribe(res => {
            resolve(JSON.parse(JSON.stringify(res)));
             console.log('json'+ JSON.stringify(postcredn));

           }, (err) => {
             reject(err);
             console.log(apiUrl);
           });
     });
    }

}           console.log(apiUrl);
           });
     });
    }

}

提前致谢

【问题讨论】:

    标签: laravel authentication ionic-framework oauth ionic3


    【解决方案1】:

    Javascript 中的关联数组在技术上是对象,不能通过new Array() 实例化。要创建一个,您可以使用new Object(){} 中的任何一个。因此,根据您发布的代码,您将其定义为var postcredn=new Object();var postcredn={};

    在您的 Laravel 代码中,您可以像访问 PHP 中的关联数组一样访问它。

    【讨论】:

      【解决方案2】:

      请重写以下代码:

      首先你需要创建interface like:

      export interface Postdata{
         username: String;
         password:String;
         grant_type:String;
         client_id: String;
         client_secret: String;
      }
      

      然后导入它并设置它的值:

      var postcredn:Postdata;
      postcredn={
        username:'karthik@abcde.in',
        password:'05550',
        grant_type:'password',
        client_id:'Outfit1548669',
        client_secret:'a10620c85033abd17716cda245'
      }
      

      最后通过post方式发布:

        return new Promise((resolve, reject) => {
            this.http.post(apiUrl, postcredn,  options)
      
             .subscribe(res => {
              resolve(JSON.parse(JSON.stringify(res)));
               console.log('json'+ JSON.stringify(postcredn));
      
             }, (err) => {
               reject(err);
               console.log(apiUrl);
             });
       });
      

      【讨论】:

      • 在尝试此代码时。它显示“TypeError:”postcredn is undefined“”@Utpaul
      • codepen.io/Gokulanathan/pen/JzzGNE 请在此链接上查看我的代码。
      • 更新后错误依旧。请看上面的链接
      • 请更新代码postcredn。我已经检查过了,我的 console.log 给了我正确的输出
      猜你喜欢
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 2018-07-03
      • 2018-03-15
      • 2018-12-01
      • 1970-01-01
      • 2018-02-25
      相关资源
      最近更新 更多