Nativescript,方法帖子

时间:2018-06-13 15:49:23

标签: angular post parameters nativescript

我是新的移动开发者。我正在使用Nativescript。现在我的post method出了问题。我想在我的产品中显示一些细节,为此我得到了产品的ID,并且返回给我详细信息。

为此,我尝试使用此服务代码:

service.ts

import { Injectable } from '@angular/core';
import { Http, Headers, Response , URLSearchParams} from '@angular/http';
import { Api } from '../state/Api';
import { Product} from '~/model/product';
import 'rxjs/operators';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable()
export class ProductService {

    constructor(private http: Http,
    ) { }

    public Productgetbyid(id: string): Observable<Product> {
        let headers = new Headers();
        headers.append('Content-Type', 'application/x-www-form-urlencoded');
        let urlSearchParams = new URLSearchParams();
        urlSearchParams.append('id', id);
        urlSearchParams.append('id_unique', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9');
        let body = urlSearchParams.toString();
        console.log('body',urlSearchParams.toString())
        return this.http.post(Api.getUrl(Api.URLS.Productgetbyid), body, {
            headers: headers
        })
            .pipe(map((response: Response) => {
                let res = response.json();
                if (res.StatusCode === 0) {
                    return new Product(res.StatusDescription[0]);
                } else if (res.StatusCode === 1) {
                } else {
                    return new Product(null);
                }
            }));
    }
}

型号产品:

export class Product{
    id: string;
    prodnumber: String;
    name: string;

    constructor(obj: any) {
        this.id= obj.id;
        this.prodnumber= obj.prodnumber;
        this.name= obj.name;
    }
}

这段代码用我的代码调用我的组件:

component.ts

    items: Product;
    public ngOnInit() {
     this.route.params.subscribe(
            params => {
                console.log('params',params)
                this.as.Productgetbyid(params['id']).subscribe(
                    items=> {
                        console.log('items',items)
                        this.items= items;
                    }
                );
            }
        );
}

component.html

<StackLayout class="page">
    <ListView [items]="items" class="list-group">
        <ng-template let-item="item">
            <Label [nsRouterLink]="['/item', item.id]" [text]="item.name"
                class="list-group-item"></Label>
        </ng-template>
    </ListView>
</StackLayout>

我的ID未定义。

从API我期望这个JSON:

{
    "StatusCode": 0,
    "StatusMessage": "OK",
    "StatusDescription": [
        {
            "id": 5,
            "prodnumber": 4,
            "name": "UpdateTest"
        }
    ]
}

的package.json

{
    "description": "NativeScript Application",
    "license": "SEE LICENSE IN <your-license-filename>",
    "readme": "NativeScript Application",
    "repository": "<fill-your-repository-here>",
    "nativescript": {
        "id": "org.nativescript.project",
        "tns-android": {
            "version": "4.1.2"
        }
    },
    "dependencies": {
        "@angular/animations": "~6.0.0",
        "@angular/common": "~6.0.0",
        "@angular/compiler": "~6.0.0",
        "@angular/core": "~6.0.0",
        "@angular/forms": "~6.0.0",
        "@angular/http": "~6.0.0",
        "@angular/platform-browser": "~6.0.0",
        "@angular/platform-browser-dynamic": "~6.0.0",
        "@angular/router": "~6.0.0",
        "nativescript-angular": "~6.0.0",
        "nativescript-barcodescanner": "^2.7.6",
        "nativescript-theme-core": "~1.0.4",
        "reflect-metadata": "~0.1.8",
        "rxjs": "~6.1.0",
        "tns-core-modules": "~4.1.0",
        "zone.js": "^0.8.26"
    },
    "devDependencies": {
        "babel-traverse": "6.4.5",
        "babel-types": "6.4.5",
        "babylon": "6.4.5",
        "lazy": "1.0.11",
        "nativescript-dev-typescript": "~0.7.0",
        "typescript": "~2.7.2"
    }
}

请问您有什么想法,如何发布我正确的身份证明?

感谢名单

0 个答案:

没有答案