Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 49247

'http://localhost:8100' has been blocked by CORS policy

$
0
0

@sahibsingh wrote:

I am using Ionic 4 and facing the CORS Issue problem. I want to access mysql using php, but it is blocked by CORS, i have add all the headers in the php file for permissions but problem still exist.

file_aksi.php file

<?php header('Access-Control-Allow-Origin: http://localhost:8100'); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE'); header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization'); header('Content-Type: application/json; charset=UTF-8'); include "config.php"; $postjson = json_decode(file_get_contents('php://input'), true); $today = date('Y-m-d'); if($postjson['aksi'] == "add_register") { $password = md5($postjson['password']); $query = mysqli_query($mysqli, "INSERT INTO login SET full_name = '$postjson[full_name]', phone_number = '$postjson[phone_number]', username = '$postjson[username]', password = '$password' "); if($query) $result = json_encode(array('success' =>true)); else $result = json_encode(array('success' => false, 'msg'=>'error , please try again')); echo $result; } elseif($postjson['aksi'] == "login") { $password = md5($postjson['password']); $query = mysqli_query($mysqli, "SELECT * FROM login WHERE username='$postjson[username]' AND password='$password' "); $check = mysqli_num_rows($query); if($check>0){ $data = mysqli_fetch_array($query); $datauser = array( 'user_id' => $data['user_id'], 'full_name' => $data['full_name'], 'phone_number' => $data['phone_number'], 'username' => $data['username'], 'password' => $data['password'] ); if($query) $result = json_encode(array('success' =>true, 'result'=>$datauser)); else $result = json_encode(array('success' => false, 'msg'=>'error, please try again')); }else{ $result = json_encode(array('success' => false, 'msg'=>'unregister account')); } echo $result; } if($postjson['aksi']=='add'){ $query = mysqli_query($mysqli, "INSERT INTO master_customer SET name_customer = '$postjson[name_customer]', desc_customer = '$postjson[desc_customer]', created_at = '$today' "); $idcust = mysqli_insert_id($mysqli); if($query) $result = json_encode(array('success'=>true, 'customerid'=>$idcust)); else $result = json_encode(array('success'=>false)); echo $result; } elseif($postjson['aksi']=='getdata'){ $data = array(); $query = mysqli_query($mysqli, "SELECT * FROM master_customer ORDER BY customer_id DESC LIMIT $postjson[start],$postjson[limit]"); while($row = mysqli_fetch_array($query)){ $data[] = array( 'customer_id' => $row['customer_id'], 'name_customer' => $row['name_customer'], 'desc_customer' => $row['desc_customer'], 'created_at' => $row['created_at'], ); } if($query) $result = json_encode(array('success'=>true, 'result'=>$data)); else $result = json_encode(array('success'=>false)); echo $result; } elseif($postjson['aksi']=='update'){ $query = mysqli_query($mysqli, "UPDATE master_customer SET name_customer='$postjson[name_customer]', desc_customer='$postjson[desc_customer]' WHERE customer_id='$postjson[customer_id]' "); if($query) $result = json_encode(array('success'=>true, 'result'=>'success')); else $result = json_encode(array('success'=>false, 'result'=>'error')); echo $result; } elseif($postjson['aksi']=='delete'){ $query = mysqli_query($mysqli, "DELETE FROM master_customer WHERE customer_id='$postjson[customer_id]' "); if($query) $result = json_encode(array('success'=>true, 'result'=>'success')); else $result = json_encode(array('success'=>false, 'result'=>'error')); echo $result; } ?>

config.php file

<?php define('DB_NAME', 'ionic4login'); define('DB_USER', 'root'); define('DB_PASSWORD', ''); define('DB_HOST', 'localhost'); $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); ?>

register.page.ts file

import { Component, OnInit } from ‘@angular/core’;

import { Router } from ‘@angular/router’;

import { ToastController } from ‘@ionic/angular’;

import { PostProvider } from ‘…/…/providers/post-provider’;

import { async } from ‘q’;

@Component({

selector: ‘app-register’,

templateUrl: ‘./register.page.html’,

styleUrls: [’./register.page.scss’],

})

export class RegisterPage {

full_name: string = ‘’;

phone_number: string = ‘’;

username: string = ‘’;

password: string = ‘’;

confirm_password: string = ‘’;

constructor(

private router: Router,

public toastController: ToastController,

private postPvdr: PostProvider,

) { }

ngOnInit() {

}

formLogin() {

this.router.navigate(['/login']);

}

async addRegister() {

if (this.full_name == '') {

  const toast = await this.toastController.create({

  message: 'Fullname is required',

  duration: 2000

  });

  toast.present();

} else if (this.phone_number == '') {

  const toast = await this.toastController.create({

    message: 'Phone number is required',

    duration: 2000

    });

  toast.present();

} else if (this.username == '') {

  const toast = await this.toastController.create({

    message: 'Username is required',

    duration: 2000

    });

  toast.present();

} else if (this.password == '') {

  const toast = await this.toastController.create({

    message: 'Password is required',

    duration: 2000

    });

  toast.present();

} else if (this.password != this.confirm_password) {

  const toast = await this.toastController.create({

    message: 'Password does not match',

    duration: 2000

    });

  toast.present();

} else {

  let body = {

    full_name: this.full_name,

    phone_number: this.phone_number,

    username: this.username,

    password: this.password,

    aksi: 'add_register'

  };

  this.postPvdr.postData(body, 'file_aksi.php').subscribe(async data => {

   var alertpesan = data.msg;

   if (data.success) {

     this.router.navigate(['/login']);

     const toast = await this.toastController.create({

      message: 'Register successfully',

      duration: 2000

     });

     toast.present();

   } else {

     const toast = await this.toastController.create({

       message: alertpesan,

       duration: 2000

     });

   }

 });

}

}

}

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 49247

Trending Articles



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