# Post method in ionic 3

**URL:** <https://forum.ionicframework.com/t/post-method-in-ionic-3/169534>\
**Category:** ionic-v3\
**Created:** [July 24, 2019, 7:56am UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534 "2019-07-24T07:56:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tubiss](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/tubiss/32/127043_2.png) [@Tubiss](https://forum.ionicframework.com/u/Tubiss)\
**Post date:** [July 24, 2019, 7:56am UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534/1 "2019-07-24T07:56:30Z")

</div>

ı have laravel json api and works well when ı made auth in laravel post method work nice. but in ionic my codes didnt work.

this is provider

```auto
 postData(credentials, type){

    return new Promise((resolve, reject) =>{
      let headers = new Headers({
        'Content-Type' : 'application/json'
      });
      let options = new RequestOptions({ headers: headers });
      
    this.http.post(apiUrl+type, JSON.stringify(credentials), {headers: headers}).
      subscribe(res =>{
        resolve(res.json());
      }, (err) =>{
        reject(err);
      });

    });

  }

```

signup.ts

```auto
export class SignUpPage {
  resposeData : any;
  userData = {"full_name":"", "password":"","email":"","name":""};
 

  constructor(public navCtrl: NavController, public authProvider:AuthProvider, 
    public alertCtrl: AlertController , public toastCtrl:ToastController
    ) {
  }

signup() {
    if(this.userData.full_name && this.userData.password && this.userData.email && this.userData.name){
      //Api connections
    this.authProvider.postData(this.userData, "signup").then((result) =>{
    this.resposeData = result;
    if(this.resposeData.userData){
      console.log(this.resposeData);
      localStorage.setItem('userData', JSON.stringify(this.resposeData) )
      this.navCtrl.push(HomePage);
    }
    else{
      this.presentToast("Please give valid username and password");
    }
    
    }, (err) => {
      console.log("error");
    });
  }
  else {
    console.log("Give valid information.");
  }
  
  }

  login() {
    this
      .navCtrl
      .push(SignInPage);
  }

  presentToast(msg) {
    let toast = this.toastCtrl.create({
      message: msg,
      duration: 2000
    });
    toast.present();
  }

```

and when ı make auth in ionic ı am getting this error.

**access to XMLHttpRequest at ‘http://…8000/api/signup’ from origin ‘[http://localhost](http://localhost)’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.**

so please help me.

---

<div class="post-metadata">

**Author:** ![gokujy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/gokujy/32/117753_2.png) [@gokujy](https://forum.ionicframework.com/u/gokujy)\
**Post date:** [July 24, 2019, 2:20pm UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534/2 "2019-07-24T14:20:56Z")

</div>

If you are using chrome then add this extension : [Allow-Control-Allow-Origin](https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en)

then add host URL(ex. [http://localhost:8100/](http://localhost:8100/)) in extension and refresh if stay same issue remove URL from chrome and refresh your page

---

<div class="post-metadata">

**Author:** ![saad-ansari](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/saad-ansari/32/130182_2.png) [@saad-ansari](https://forum.ionicframework.com/u/saad-ansari)\
**Post date:** [July 24, 2019, 5:58pm UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534/3 "2019-07-24T17:58:19Z")

</div>

> [@Tubiss](#):
>
> laravel

corss-Origin extension works olny localhost when you are publish your app your api dosent work well

make sure cross origin allow in your laravel api  
@Tubiss

---

<div class="post-metadata">

**Author:** ![Tubiss](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/tubiss/32/127043_2.png) [@Tubiss](https://forum.ionicframework.com/u/Tubiss)\
**Post date:** [July 25, 2019, 6:42am UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534/4 "2019-07-25T06:42:31Z")

</div>

but other data which in the json is display on the chrome.I have problem only signup and signin

---

<div class="post-metadata">

**Author:** ![bundayy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bundayy/32/127402_2.png) [@bundayy](https://forum.ionicframework.com/u/bundayy)\
**Post date:** [July 25, 2019, 2:02pm UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534/6 "2019-07-25T14:02:34Z")

</div>

Hi, you are having a CORS issue, since you are using Laravel install [Laravel CORS Package](https://packagist.org/packages/barryvdh/laravel-cors) this will resolve your CORS issues and your app will be fine

---

<div class="post-metadata">

**Author:** ![Tubiss](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/tubiss/32/127043_2.png) [@Tubiss](https://forum.ionicframework.com/u/Tubiss)\
**Post date:** [July 25, 2019, 2:31pm UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534/7 "2019-07-25T14:31:19Z")

</div>

Still same error **polyfills.js:3 POST localhost/api/student 405 (Method Not Allowed)**

**Access to XMLHttpRequest at ‘localhostapi/student’ from origin ‘http:/localhost’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.**

please help me .

---

<div class="post-metadata">

**Author:** ![bundayy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bundayy/32/127402_2.png) [@bundayy](https://forum.ionicframework.com/u/bundayy)\
**Post date:** [July 25, 2019, 2:34pm UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534/8 "2019-07-25T14:34:21Z")

</div>

hi, after installing the package, can i see your `config/cors.php` content?

---

<div class="post-metadata">

**Author:** ![Tubiss](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/tubiss/32/127043_2.png) [@Tubiss](https://forum.ionicframework.com/u/Tubiss)\
**Post date:** [July 26, 2019, 6:38am UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534/9 "2019-07-26T06:38:56Z")

</div>

**config/cors.php**

```auto
<?php

return [

    'supportsCredentials' => false,

    'allowedOrigins' => ['*'],

    'allowedHeaders' => ['Content-Type', 'X-Requested-With'],

    'allowedMethods' => ['*'], // ex: ['GET', 'POST', 'PUT', 'DELETE']

    'exposedHeaders' => [''],

    'maxAge' => 0,
];

```

---

<div class="post-metadata">

**Author:** ![bundayy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bundayy/32/127402_2.png) [@bundayy](https://forum.ionicframework.com/u/bundayy)\
**Post date:** [July 26, 2019, 7:29am UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534/10 "2019-07-26T07:29:14Z")

</div>

So `Access-Control-Allow-Origin` is needed in the `allowedHeaders`.

Update your `allowedHeaders` to `['Content-Type', 'X-Requested-With', 'Access-Control-Allow-Origin']` to allow it.

Or alternatively, update your header to `['*']` to allow all header

---

<div class="post-metadata">

**Author:** ![Tubiss](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/tubiss/32/127043_2.png) [@Tubiss](https://forum.ionicframework.com/u/Tubiss)\
**Post date:** [July 26, 2019, 7:41am UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534/11 "2019-07-26T07:41:12Z")

</div>

still same giving error. dont know where ı am wrong . I can make auth in the laravel login and my login info is inserted to database . But in the ionic ı always getting this error so please help me.

---

<div class="post-metadata">

**Author:** ![bundayy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bundayy/32/127402_2.png) [@bundayy](https://forum.ionicframework.com/u/bundayy)\
**Post date:** [July 26, 2019, 7:46am UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534/13 "2019-07-26T07:46:18Z")

</div>

Hi, Please share more details so i can help

---

<div class="post-metadata">

**Author:** ![Tubiss](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/tubiss/32/127043_2.png) [@Tubiss](https://forum.ionicframework.com/u/Tubiss)\
**Post date:** [July 26, 2019, 7:55am UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534/14 "2019-07-26T07:55:32Z")

</div>

ok ı am going to explain what ı did.

ı made auth with different table(student table) instead of default user table.  
And ı can make login in the laravel auth with student table and my info is inserted to database.  
Everything is fine until now.

And ı have been insert some auth code in ionic app and ı cannot make login.  
By the way my api works well because ı can pull json data and ı can display json data in the ionic app. But can not make post method.

---

<div class="post-metadata">

**Author:** ![bundayy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bundayy/32/127402_2.png) [@bundayy](https://forum.ionicframework.com/u/bundayy)\
**Post date:** [July 26, 2019, 8:13am UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534/15 "2019-07-26T08:13:06Z")

</div>

Ok, confirm that the route you are trying to post to exist and you can post to it through Postman

---

<div class="post-metadata">

**Author:** ![bundayy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bundayy/32/127402_2.png) [@bundayy](https://forum.ionicframework.com/u/bundayy)\
**Post date:** [July 26, 2019, 8:17am UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534/16 "2019-07-26T08:17:59Z")

</div>

> [@Tubiss](#):
>
> POST localhost/api/student 405 (Method Not Allowed)

looking at this 405, it means that the `/student` route does not allow POST method

---

<div class="post-metadata">

**Author:** ![Tubiss](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/tubiss/32/127043_2.png) [@Tubiss](https://forum.ionicframework.com/u/Tubiss)\
**Post date:** [July 26, 2019, 8:20am UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534/17 "2019-07-26T08:20:27Z")

</div>

yes ı cannot post in the postman

---

<div class="post-metadata">

**Author:** ![bundayy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bundayy/32/127402_2.png) [@bundayy](https://forum.ionicframework.com/u/bundayy)\
**Post date:** [July 26, 2019, 8:21am UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534/18 "2019-07-26T08:21:23Z")

</div>

okay, that mean in your laravel api, you haven’t create a POST route. so do that first and try again

---

<div class="post-metadata">

**Author:** ![Tubiss](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/tubiss/32/127043_2.png) [@Tubiss](https://forum.ionicframework.com/u/Tubiss)\
**Post date:** [July 26, 2019, 8:43am UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534/19 "2019-07-26T08:43:44Z")

</div>

ı made a function as called store in the studentController

```auto
 public function store(Request $request)
    {
		
        $full_name = $request->full_name;
		$email = $request->email;
		 $password = $request->password;

        //
    }

```

and api.php

`Route::post("/studentpost", "StudentController@store");`

and in the postman when ı try its not giving error and giving nothing and ı checked my database its not inserted.

 ![postroute](https://global.discourse-cdn.com/ionicframework/original/3X/4/0/40f63a5af287dca7957be53f52420c74e3d7a818.png)

---

<div class="post-metadata">

**Author:** ![bundayy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bundayy/32/127402_2.png) [@bundayy](https://forum.ionicframework.com/u/bundayy)\
**Post date:** [July 26, 2019, 8:49am UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534/20 "2019-07-26T08:49:02Z")

</div>

okay, good. looks like everything is fine now, in your `store` function insert the content into the db, then you will be able to see it

---

<div class="post-metadata">

**Author:** ![Tubiss](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/tubiss/32/127043_2.png) [@Tubiss](https://forum.ionicframework.com/u/Tubiss)\
**Post date:** [July 26, 2019, 10:34am UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534/21 "2019-07-26T10:34:40Z")

</div>

ı am not sure is it true. I made insert content db like this if its wrong please tell me.

```auto
	 public function store(Request $request)
    {
		
		 $student = new Student();
		
       $student->full_name = request(full_name);
 $student->email = request(email);
		 $student->password = request(password);
		 $student->save();

        //
    }

```

and when ı made this ı got the error from postman like this

**ErrorException: Use of undefined constant full\_name - assumed 'full\_name' (this will throw an Error in a future version of PHP) in**

---

<div class="post-metadata">

**Author:** ![bundayy](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bundayy/32/127402_2.png) [@bundayy](https://forum.ionicframework.com/u/bundayy)\
**Post date:** [July 26, 2019, 10:54am UTC](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534/22 "2019-07-26T10:54:04Z")

</div>

Okay, follow the following steps

import this in your controller file

```auto
use Illuminate\Support\Facades\Hash;

```

then in your `store` method write:

```auto
$student = new Student();
$student->full_name = $request->full_name;
$student->email = $request->email;
$student->password = Hash::make($request->password);
$student->save();

return response()->json(['status'=>'success','message'=> 'Student Created']);

```

It should work now

Note: you dont save password as plain text, that is why you need to Hash it.

[Next page](https://forum.ionicframework.com/t/post-method-in-ionic-3/169534.md?page=2)
