Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 70435

Ionic 5 http request returns only "undefined"

$
0
0

Ok, so I need to send a post request to the server, this is my api.service.ts code:

import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { HTTP } from '@ionic-native/http/ngx';
constructor(public platform: Platform, private http: HttpClient, private config: Config, private ionicHttp: HTTP) {
postItem(endPoint, data = {}, path = '/'){
    const url = this.config.url + path + 'wp-admin/admin-ajax.php?action=mstoreapp-' + endPoint;
    var params = new HttpParams();
    for (var key in data) { if('object' !== typeof(data[key])) params = params.set(key, data[key]) }
    params = params.set('lang', this.config.lang);
    return new Promise((resolve, reject) => {
        this.http.post(url, params, this.config.options).pipe(map((res: any) => res)).subscribe(data => {
	    resolve(data);
	}, err => {
	    reject(err.error);
	});
    });
}

When I call it like this:

this.api.postItem('store', {'store_id': this.id}).then(res => {
    console.log( res );
}, err => {
    console.log(err);
});

I get this result:

It works fine.
However, when I change it to this:

postItem(endPoint, data = {}, path = '/'){
    const url = this.config.url + path + 'wp-admin/admin-ajax.php?action=mstoreapp-' + endPoint;
    data['lang'] = this.config.lang;
    this.ionicHttp.setHeader(this.options, 'Content-Type', 'application/json; charset=UTF-8');
    this.ionicHttp.setDataSerializer('urlencoded');
    return new Promise((resolve, reject) => {
        this.ionicHttp.post(url, data, {})
	  .then(data => {
            console.log(data.data);
            resolve(JSON.parse(data.data));
	  })
	  .catch(error => {
            console.log(error.error);
            reject(JSON.parse(error.error));
	  });
    });
}

I get this result:


It only returns “undefined”
Code on the server side:
    public function getStore() {
        
        global $wpdb;
        
        $id = $_REQUEST['store_id'];
        $store = $wpdb->get_results( "
            SELECT * FROM $wpdb->posts AS p
            WHERE p.post_type='stores' AND p.ID='$id'
        ", ARRAY_A );

        wp_send_json( $store );
}

This is ionic info

Ionic:

   Ionic CLI                     : 6.9.2 (C:\Users\matve\AppData\Roaming\npm\node_modules\@ionic\cli)
   Ionic Framework               : @ionic/angular 5.0.0
   @angular-devkit/build-angular : 0.803.25
   @angular-devkit/schematics    : 8.3.25
   @angular/cli                  : 8.3.25
   @ionic/angular-toolkit        : 2.1.2

Capacitor:

   Capacitor CLI   : 2.1.0
   @capacitor/core : 2.1.0

Cordova:

   Cordova CLI       : 9.0.0 (cordova-lib@9.0.1)
   Cordova Platforms : android 8.1.0, browser 6.0.0
   Cordova Plugins   : cordova-plugin-ionic-keyboard 2.2.0, cordova-plugin-ionic-webview 4.1.3, (and 17 other plugins)

Utility:

   cordova-res : 0.14.0
   native-run  : 1.0.0

System:

   Android SDK Tools : 26.1.1 (C:\Users\matve\AppData\Local\Android\Sdk)
   NodeJS            : v13.3.0 (C:\Program Files\nodejs\node.exe)
   npm               : 6.13.1
   OS                : Windows 10

Could you suggest what could be a cause of this problem, and how to solve it? I’ll appreciate it a

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 70435

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>