ERROR TypeError: alert is not a function
at SafeSubscriber._error (home.ts:45)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:238)
at SafeSubscriber.error (Subscriber.js:197)
at Subscriber._error (Subscriber.js:128)
at Subscriber.error (Subscriber.js:102)
at MapSubscriber._next (map.js:82)
at MapSubscriber.Subscriber.next (Subscriber.js:89)
at XMLHttpRequest.onLoad (http.js:1556)
at t.invokeTask (polyfills.js:3)
at Object.onInvokeTask (core.js:4620)
Iām receiving the otp but page is not redirecting to the otp receive page because of this error
below is my code
import { Component } from '@angular/core';
import { NavController,Platform,AlertController } from 'ionic-angular';
import {Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
import { OtpReceivePage } from '../otp-receive/otp-receive';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
mobile='';
constructor(public alertCtrl: AlertController,
public http:Http,
public navCtrl:NavController) {
}
sendOTP()
{
if(this.mobile.length!=12)
{
let alert = this.alertCtrl.create({
title: 'Mobile Number Required!',
subTitle: 'Please enter your 10 digit mobile number with 91 country code!',
buttons: ['OK']
});
alert.present();
}
else
{
this.http.get('http://localhost:8080/nexmosms/send-sms.php')
.map(res => res.json())
.subscribe(res => {
console.log(JSON.stringify(res));
this.navCtrl.push(OtpReceivePage,{mobileno:this.mobile})
}, (err) => {
alert("failed");
});
}
}
}
And here, youāre using āalertā as a function. I know your intention was probably the default js alert() function. But the compiler is thinking youāre talking about the above alert which is indeed not a function.
Just change the variable name āalertā to something else and youāll be good.
If your āelseā clause is not executed, then the page will not redirect to āOtpReceivePageāā¦ And the way to see that is by this line below. If you see the data on your browser console, then the page should redirect. Can you see the āresā you console logged?
No, I didnāt mean stick the line in the Console. I meant when you run your app and enter the 12 digit numbers and click āOkā or āSubmitā, do you get or see any data on the browser console after that or you get an alert with āfailedā?
Thatās exactly what I thought, your āOptReceivePageā isnāt the problem. If āhttp:ā¦localhost:8080ā is running then your problem is on your āsend-sms.phpā file.
Replace the line below with console.log(err); the console will give more details on what the problem is.
SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at Response.Body.json (http.js:1063)
at MapSubscriber.project (home.ts:39)
at MapSubscriber._next (map.js:79)
at MapSubscriber.Subscriber.next (Subscriber.js:89)
at XMLHttpRequest.onLoad (http.js:1556)
at t.invokeTask (polyfills.js:3)
at Object.onInvokeTask (core.js:4620)
at t.invokeTask (polyfills.js:3)
at r.runTask (polyfills.js:3)
Looks like your āsend-sms.phpā isnāt returning the correct data format (JSON). And it needs to return JSON for this to work. Whatever your file is returning begins with ā<ā and it could be XML or HTML.
You need your function on your PHP file to return json data format.
Sorry dude, Iāve never used PHP in my life. But if you wanna see your Response (returned data). Run your app, before entering the 12 numbers and firing the request (Send)
Click F12 to go to browser dev tools, click the Network tab.
Enter the 12 digit number on your form and submitā¦ On your Network click on the last requestās name, its gonna open another window, then click on āReponseā and you should be able to see where you went wrongā¦ Example below