如何在Ionic 2中使用x-www-form-urlencoded发送Http Post参数

时间:2018-03-08 10:33:05

标签: ionic-framework ionic2

我是Ionic的初学者2.我想从app进行用户注册。对于用户注册,我使用api,其中 POST ,其媒体类型为 x-www-form -urlencoded 即可。我想将数据发送到服务器,但我无法成功发送请求。

我需要一些帮助。[![在此输入图像说明] [1]] [1]

在我的 POST 请求中,有 9参数 他们是:

1.officername 2.officename 3.designation 4.emailed 5.landlinecode 6.landlinenumber 7.mobilenumber 8.password 9.status

这是我的代码

 doLogin() {
   let url = "http:////Registration";

  var headers = new Headers()
   headers.append('Content-Type', 'application/x-www-form-urlencoded');

  let options = new RequestOptions({ headers: headers });

   let body= JSON.stringify({ officme: 'mr',emled:'adminac.com', mobilenumber:'9025225',
    password:'12345',offm:'mrc',desigtion:'prrammer',landlinecode:'0000',
   landlinenumber:'888888',status:'A'});
   alert(body);


  return this.http.post(url, body, options) .map(res => res.json()).subscribe(
              data => {
                console.log(data);
              },
              err => {
                console.log("ERROR!: ", err);
              }
          );
        }
      }

2 个答案:

答案 0 :(得分:1)

请替换此代码,我希望它能按照您的要求运作

doLogin() {
   let url = "http:////Registration";

  var headers = new Headers()
   headers.append('Content-Type', 'application/x-www-form-urlencoded');

  let options = new RequestOptions({ headers: headers });


    let body =  "offiername=" +'mr' + "&emailed=" + 'admin@mac.com' + "&mobilenumber=" + '9025225' + "&password=" + '12345' + "&officename=" + 'mrc' + "&designation=" + 'programmer' +"&landlnecode=" + '0000' +"&landlinenumber=" + '888888' +"&sttus=" + 'A';

    alert(body);

  return this.http.post(url, body, options) .map(res => res.json()).subscribe(
              data => {
                console.log(data);
              },
              err => {
                console.log("ERROR!: ", err);
              }
          );
        }
      }

答案 1 :(得分:0)

尝试以下代码:

 let url = "http://host//Registration";

    let headers = new Headers();
    headers.append('Access-Control-Allow-Origin' , '*');
    headers.append('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT');
    headers.append('Accept','application/json');
    headers.append('content-type','application/json');
      let options = new RequestOptions({ headers:headers});
   return new Promise((resolve,reject)=>{


      let body= JSON.stringify({ officername: 'mrsac',emailed:'admin@mrc.com', mobilenumber:'902160',password:'12345',officename:'mr',designation:'programmer',landlinecode:'0000',
   landlinenumber:'888888',status:'A'});

           this.http.post(url,JSON.stringify(body), options).subscribe(res => {
              resolve(res.json());
            }, (err) => {
              reject(err);
            });

   })