2017-08-02 84 views
0

我是混合應用程序開發中的新人。我正在使用Ionic cordova構建我的應用程序。我正嘗試在我的應用程序中調用post方法。Ionic Cordova Post方法不起作用

var url = "http://XXXXXXXXXXXXX.com/XXXX"; 
var headers = new Headers(); 
headers.append("Accept",'application/json'); 
    headers.append('Content-Type','application/json'); 
    let options = new RequestOptions({headers : headers}); 

    let postParams = { 
    Name: this.imagename, 
    StartDate: this.adstartdate, 
    EndDate: this.adEnddate, 
    UploadName:this.imagename 
    } 

//上面的代碼(postParams)沒有附加下面的代碼。

this.http.post(url,postParams,options) 
.subscribe(data =>{ 
    console.log(data['_body']); 
    alert('Sucess Enter'+ data['_body']); 
    this.jsonvalue = JSON.stringify(data['_body']); 


    alert('Json Value : '+ this.jsonvalue); 

},error =>{ 

    console.log(error); 
    alert('Fail'); 
} 

我想知道是我在這段代碼中做錯了。 請忽略我的語法錯誤。

+0

你有像500或404的任何錯誤或錯誤代碼上面的選擇? – hrdkisback

+0

不,我沒有收到任何錯誤代碼。 –

+0

@hrdkisback能夠打api,但是我不能附上標題 –

回答

0

您需要發送RequestOptions對象作爲參數post方法。

例如

import {RequestOptions} from '@angular/http' 

然後

let opt1 = new RequestOptions({ 
       Name: this.imagename, 
       StartDate: this.adstartdate, 
       EndDate: this.adEnddate, 
       UploadName:this.imagename 
       }); 

使用HTTP請求

this.http.post(url,postParams,opt) //here 
.subscribe(data =>{ 
    console.log(data['_body']); 
    alert('Sucess Enter'+ data['_body']); 
    this.jsonvalue = JSON.stringify(data['_body']); 


    alert('Json Value : '+ this.jsonvalue); 

},error =>{ 

    console.log(error); 
    alert('Fail'); 
}