Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
离子型3-can';无法在http请求内创建警报_Http_Ionic3_Cordova Plugins - Fatal编程技术网

离子型3-can';无法在http请求内创建警报

离子型3-can';无法在http请求内创建警报,http,ionic3,cordova-plugins,Http,Ionic3,Cordova Plugins,因此,我使用本机HTTP cordova插件来处理我的HTTP请求。但是,我似乎无法在onFail函数中创建警报。还有其他人经历过吗 错误显示: Error in Error callbackId: CordovaHttpPlugin1608257770 : TypeError: Cannot read property 'alertCtrl' of undefined 下面是我如何构建代码的: cordova.plugin.http.sendRequest('http://127.0.0.

因此,我使用本机HTTP cordova插件来处理我的HTTP请求。但是,我似乎无法在onFail函数中创建警报。还有其他人经历过吗

错误显示:

Error in Error callbackId: CordovaHttpPlugin1608257770 : TypeError: Cannot 
read property 'alertCtrl' of undefined
下面是我如何构建代码的:

cordova.plugin.http.sendRequest('http://127.0.0.1:5000/api/login/', options, function(response) {

        try {   //onSuccess
            response.data = JSON.parse(response.data);

            localStorage.setItem('token', JSON.stringify(response.data.token));

          } catch(e) {
            console.error('JSON parsing error');
          }
        },  function(response) {    //onFail
            console.log('403');

            let alert = this.alerts.create({
                title: 'Error',
                subTitle: 'Username/password is invalid!',
                buttons: ['Dismiss']
            });
            alert.present();

        }
    );
下面是我的构造函数的外观:

constructor(public navCtrl: NavController,
            private alerts: AlertController, 
            private http: HTTP, 
            private store: Storage, 
            ) {}

是什么导致它无法工作?

试试这个,因为当您在请求中创建警报时,“这”意味着httprequest,所以它没有警报类

        let alert = this.alerts.create({
            title: 'Error',
            subTitle: 'Username/password is invalid!',
            buttons: ['Dismiss']
        });

cordova.plugin.http.sendRequest('http://127.0.0.1:5000/api/login/', options, function(response) {

        try {   //onSuccess
            response.data = JSON.parse(response.data);

            localStorage.setItem('token', JSON.stringify(response.data.token));

          } catch(e) {
            console.error('JSON parsing error');
          }
        },  function(response) {    //onFail
            console.log('403');
            alert.present();

        }
    );

试试这个,因为当您在请求中创建警报时,“this”意味着httprequest,所以它没有警报类

        let alert = this.alerts.create({
            title: 'Error',
            subTitle: 'Username/password is invalid!',
            buttons: ['Dismiss']
        });

cordova.plugin.http.sendRequest('http://127.0.0.1:5000/api/login/', options, function(response) {

        try {   //onSuccess
            response.data = JSON.parse(response.data);

            localStorage.setItem('token', JSON.stringify(response.data.token));

          } catch(e) {
            console.error('JSON parsing error');
          }
        },  function(response) {    //onFail
            console.log('403');
            alert.present();

        }
    );

哇!工作起来很有魅力!真不敢相信是这么简单的错误造成的。谢谢你,你是救命恩人你介意回答我贴的另一个问题吗?哇!工作起来很有魅力!真不敢相信是这么简单的错误造成的。谢谢你,你是救命恩人你介意回答我贴的另一个问题吗?