簡體   English   中英

使用angular2在ionic 2中實現后Web服務

[英]implementation of post web service in ionic 2 using angular2

我正在嘗試在angular2中實現post webservice。 我試圖通過郵遞員及其工作來訪問URL。 但是,當我嘗試以角度的方式實現它時,我沒有得到任何回應。 以下是我的代碼示例:

 load(username, password) {

        console.log(username,"  ", password);
        let postData = {"username":username, "password" : password, 
                "userdeviceInfo": [{
                "deviceId": "APA91bGUZuKVbqur7Qq2gy2eyomWgXkIU5Jcmmtmgl4IGuzVzwiJVMZgAHj3Bx6yrnW0oEZlEtB9XdcR6AOpKyEMVSWwQ_UIfNX6T0iwq28hnufOhauVdTYZQSWWPAdDrdg58cjnL5T-",
                "platform":"Android"
                          }]};

        //let body= JSON.stringify(postData); 

      //console.log("body---"+body)

        this.headers = new Headers();
            this.headers.append("Content-Type", 'application/json');

            this.requestoptions = new RequestOptions({
                method: RequestMethod.Post,
                url: this.url,
                headers: this.headers,
                body: JSON.stringify(postData)
            })

    console.log("body---"+this.requestoptions)

    return this.http.request(new Request(this.requestoptions))
                .map((res: Response) => {
                    if (res) {

                      console.log(res.json());
                        return [{ status: res.status, json: res.json() }];
                    }})
               .subscribe(res => this.data = res);

我收到的錯誤是:

XMLHttpRequest cannot load "MY_URL". Response for preflight has invalid HTTP status code 500

我有點卡在這里。 誰能幫我找到我要去哪里哪里?

這是一個POST示例:

rate(url: string, body: { value: number}) {
  let headers = new Headers({ 'Content-Type': 'application/json' });
  let options = new RequestOptions({ headers: headers });

  return this.http.post(url, body, options).toPromise().then(
          response => response.json(),
          err => err
  );
}

當然,您可以刪除toPromise()以使用可觀察對象,這是示例應用程序:)

希望這可以幫到你。

您可以使用這種方式發布http帖子:

let headers = new Headers();
let body = {"username":username, "password" : password, 
                "userdeviceInfo": [{
                "deviceId": "APA91bGUZuKVbqur7Qq2gy2eyomWgXkIU5Jcmmtmgl4IGuzVzwiJVMZgAHj3Bx6yrnW0oEZlEtB9XdcR6AOpKyEMVSWwQ_UIfNX6T0iwq28hnufOhauVdTYZQSWWPAdDrdg58cjnL5T-",
                "platform":"Android"
                          }]};

headers.append('content-type', 'application/json');
return this.http.post("theurl", '', {
    body : JSON.stringify(body),
    headers : headers
})
.map(res => res.json())
.subscribe(data=>{

},
err=>{

},
()=>{

})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM