# HTTP Post to php api returns null

**URL:** <https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615>\
**Category:** ionic-v1\
**Created:** [October 26, 2015, 10:08pm UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615 "2015-10-26T22:08:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![snehalp](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/snehalp/32/92983_2.png) [@snehalp](https://forum.ionicframework.com/u/snehalp)\
**Post date:** [October 26, 2015, 10:08pm UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615/1 "2015-10-26T22:08:48Z")

</div>

Hi,

i’m using this api [https://github.com/mevdschee/php-crud-api](https://github.com/mevdschee/php-crud-api) on my server here [http://activeengg.com/sandbox/api.php/teams/](http://activeengg.com/sandbox/api.php/teams/) , GET works fine, but POST always returns a null, when it should return the new record id.  
Tried the proxy for CORS but at times the request converts to OPTIONS.  
Also tried adding Access-Control-Allow-Origin to the api.php.

tested the API here [https://www.hurl.it/](https://www.hurl.it/)  
using [http://activeengg.com/sandbox/api.php/teams/](http://activeengg.com/sandbox/api.php/teams/) using these  
’{“sportid”: “1”, “eventid”: “1”,“teamname”:“snehal”,“active”:“1”}’;  
as parameters and it works great.

Been at this for days now, any breakthrough would be just wonderful.

Thanks  
Snehal

---

<div class="post-metadata">

**Author:** ![gensollen](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/gensollen/32/92854_2.png) [@gensollen](https://forum.ionicframework.com/u/gensollen)\
**Post date:** [October 26, 2015, 11:00pm UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615/2 "2015-10-26T23:00:00Z")

</div>

Maybe I am wrong, but it looks like you do not have a good callback. Like you are not waiting the response so the return is null. Can you show us your POST function ?

---

<div class="post-metadata">

**Author:** ![DaDanny](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dadanny/32/90685_2.png) [@DaDanny](https://forum.ionicframework.com/u/DaDanny)\
**Post date:** [October 27, 2015, 3:13am UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615/3 "2015-10-27T03:13:27Z")

</div>

Have you tried opening up dev tools in your browser and checking the Network tab?

Here you can check all requests you are making, and find the POST request to see why its failing.

The first things that come to mind are a bad formed POST request. Like gensollen mentioned, can you show us your POST request?

When I ran into this issue, one thing I did (a little more complicated but a great learning exercise) was open the network tab for where I could make the post request. In your case that would be [www.hurl.it](http://www.hurl.it). And monitor the network tab for that request.

If you click that request, you should be able to see the Headers being sent along with the request body.

Once you see how its formed, compare it to yours which is failing. Make sure you are sending all the correct headers and the formData is correctly formatted.

Finally, the POST request isn’t actually being converted to OPTIONS. Whats happening is your app is sending the OPTIONS request first, to find out if the POST request is safe to make or not. Its a little more complicated than that, but its 100% ok to be seeing an OPTIONS request before a POST request. You can also check that OPTIONS request to see if it comes back successful, and if not, it can give you a clue why.

---

<div class="post-metadata">

**Author:** ![snehalp](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/snehalp/32/92983_2.png) [@snehalp](https://forum.ionicframework.com/u/snehalp)\
**Post date:** [October 27, 2015, 9:09am UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615/4 "2015-10-27T09:09:07Z")

</div>

Hi gensollen,

here is the post function

* * *

$scope.doSubmit = function(addteam){  
$rootScope.showLoading();  
var url = “/teams”;  
$http({  
cache: false,  
url: ApiEndpoint.url+’/teams’,  
method: “POST”,  
data: JSON.stringify(addteam),  
headers: {‘Content-Type’: ‘application/x-www-form-urlencoded’ }  
}).  
success(function (data){  
// here the “data” parameter has your response data  
alert(data);  
$rootScope.showLoading();  
}  
).  
error(function () {  
// if you are here something is not going so good  
}  
)  
}

* * *

---

<div class="post-metadata">

**Author:** ![snehalp](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/snehalp/32/92983_2.png) [@snehalp](https://forum.ionicframework.com/u/snehalp)\
**Post date:** [October 27, 2015, 9:12am UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615/5 "2015-10-27T09:12:58Z")

</div>

![image](https://global.discourse-cdn.com/ionicframework/original/3X/8/d/8d7162d06a78aeab47f78c6ccba2e787e72db6db.png)

---

<div class="post-metadata">

**Author:** ![snehalp](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/snehalp/32/92983_2.png) [@snehalp](https://forum.ionicframework.com/u/snehalp)\
**Post date:** [October 27, 2015, 9:16am UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615/6 "2015-10-27T09:16:01Z")

</div>

Hi Danny,

tried hurl.it , returns the following…

 ![image](https://global.discourse-cdn.com/ionicframework/original/3X/c/c/cce9e6ac799f1771575934dd44d59aca82894607.png)

with the following parameters

‘{“sportid”: “1”, “eventid”: “1”,“teamname”:“snehal”,“active”:“1”}’;

Thanks  
Snehal

---

<div class="post-metadata">

**Author:** ![DaDanny](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dadanny/32/90685_2.png) [@DaDanny](https://forum.ionicframework.com/u/DaDanny)\
**Post date:** [October 27, 2015, 9:37am UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615/7 "2015-10-27T09:37:38Z")

</div>

So in my config.js file, which is basically

```
angular.module('YOUR_APP_MODULE')
.config(function($httpProvider){

```

I have:

```
    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];

```

Make sure you have that.

Another thing I would make sure is:

If you are running the server locally, if you are testing it on your actual device, make sure it can access that server and port.

If I need to test an endpoint from an actual device, many times, I can’t run local host. I need to deploy my server to an actual host and use that. (I use Heroku hosting for example)

There is ways to work around that, but I’m lazy and just upload the server if I really need to.

Finally,

In your POST, you are specifying the headers as:

```
Content-Type : 'application/x-www-form-urlencoded'

```

But in the successful POST (through hurl) it looks like it is setting the encoding as :

```
application/json

```

This will change how the server intrepets the request, which can cause errors.

Try changing your post request to :

```
headers: {
      'Content-Type' : 'application/json'
    }

```

Hopefully that helps. I’ll be online for the next hour or so if you need help. Then I’ll be back on tomorrow afternoon. Good luck. (I remember being stuck on something very similar for like 3 days and it sucked trying everything with nothing working. My problem was the formatting of the formData but yours looks good)

---

<div class="post-metadata">

**Author:** ![DaDanny](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dadanny/32/90685_2.png) [@DaDanny](https://forum.ionicframework.com/u/DaDanny)\
**Post date:** [October 27, 2015, 9:43am UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615/8 "2015-10-27T09:43:43Z")

</div>

You want to call

$ionicLoading.hide() in the success. No need to show the loading popup twice 😉

---

<div class="post-metadata">

**Author:** ![DaDanny](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dadanny/32/90685_2.png) [@DaDanny](https://forum.ionicframework.com/u/DaDanny)\
**Post date:** [October 27, 2015, 9:46am UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615/9 "2015-10-27T09:46:53Z")

</div>

Actually, it looks like it is working. In that screenshot, the POST request is coming back with a status code 200. Are you sure you are requesting the correct data? In your hurl example, you are including extra fields and a different teamname.

If the POST request isn’t coming back as status code 200, then it could be something wrong. I’d make sure you are passing the same parameters before changing the request first.

---

<div class="post-metadata">

**Author:** ![snehalp](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/snehalp/32/92983_2.png) [@snehalp](https://forum.ionicframework.com/u/snehalp)\
**Post date:** [October 27, 2015, 10:28am UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615/10 "2015-10-27T10:28:51Z")

</div>

Hi Danny,

1)added the config, no change  
2)server is not local, its here [http://activeengg.com/sandbox/api.php/teams/](http://activeengg.com/sandbox/api.php/teams/) the api code sourced from here [https://github.com/mevdschee/php-crud-api](https://github.com/mevdschee/php-crud-api), do u know a different/better one i’ll try changing that too.  
3)changing to header to ‘Content-Type’ : ‘application/json’ returns 404 with this error  
XMLHttpRequest cannot load [http://activeengg.com/sandbox/api.php/teams](http://activeengg.com/sandbox/api.php/teams). Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘[http://localhost:8100](http://localhost:8100)’ is therefore not allowed access. The response had HTTP status code 404.

Thanks a lot for all the help thus far

---

<div class="post-metadata">

**Author:** ![DaDanny](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dadanny/32/90685_2.png) [@DaDanny](https://forum.ionicframework.com/u/DaDanny)\
**Post date:** [October 27, 2015, 10:44am UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615/11 "2015-10-27T10:44:57Z")

</div>

No problem, I love helping 🙂

How are you running your app? Via ionic serve or an actual device? It looks like you are running it via ionic serve.  
I think it should work on an actual device.

---

<div class="post-metadata">

**Author:** ![snehalp](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/snehalp/32/92983_2.png) [@snehalp](https://forum.ionicframework.com/u/snehalp)\
**Post date:** [October 27, 2015, 10:46am UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615/12 "2015-10-27T10:46:03Z")

</div>

running it via ionic serve now, actual device u mean Ionic View?

---

<div class="post-metadata">

**Author:** ![snehalp](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/snehalp/32/92983_2.png) [@snehalp](https://forum.ionicframework.com/u/snehalp)\
**Post date:** [October 27, 2015, 10:48am UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615/13 "2015-10-27T10:48:37Z")

</div>

Ionic View , returns null a well

---

<div class="post-metadata">

**Author:** ![DaDanny](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dadanny/32/90685_2.png) [@DaDanny](https://forum.ionicframework.com/u/DaDanny)\
**Post date:** [October 27, 2015, 10:52am UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615/14 "2015-10-27T10:52:05Z")

</div>

Oh I think that may be the issue. The server probably has some access control headers setup. That’s why the request works when you try from hurl since the request is coming from an actual site whereas if you try it via localhost, it’s not an actual site so it will fail.

You can try using ionic view, though I’m not too experienced with that.

What I normally do is ionic add platform {iod/androdi}

Then ionic build {ios/android}

Then deploy it to a device. Either through Xcode or adb.

I’m pretty sure that’s the issue. Once you make the request via a real device, I think you’ll be fine.

**Edit**

Or if you can change the server look up changing the access control headers. I can help you if it’s node.js but I’m not too familiar with php.

---

<div class="post-metadata">

**Author:** ![snehalp](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/snehalp/32/92983_2.png) [@snehalp](https://forum.ionicframework.com/u/snehalp)\
**Post date:** [October 27, 2015, 1:15pm UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615/15 "2015-10-27T13:15:35Z")

</div>

i am using the iOS emulator now and still the same, should i remove the proxy from ionic.project?

should i may be try a different php code to fetch my mysql db as a api?

---

<div class="post-metadata">

**Author:** ![DaDanny](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dadanny/32/90685_2.png) [@DaDanny](https://forum.ionicframework.com/u/DaDanny)\
**Post date:** [October 27, 2015, 7:22pm UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615/16 "2015-10-27T19:22:51Z")

</div>

Hmmm… I’m starting to feel your frustration 😉 haha.

I’ve never used a proxy with an ionic project before, so I can’t say for certain if thats causing the issue. I think it could be, so yes, try removing it.

If that doesn’t work, and you don’t have access to the server, you can try hitting one of my servers and we can see whats wrong.

Cheers,  
Danny

---

<div class="post-metadata">

**Author:** ![gensollen](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/gensollen/32/92854_2.png) [@gensollen](https://forum.ionicframework.com/u/gensollen)\
**Post date:** [October 27, 2015, 7:36pm UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615/17 "2015-10-27T19:36:40Z")

</div>

Re,

On my side I am using http and post also like this :

> $http({  
> url: ‘url’,  
> method: ‘post’,  
> headers: {  
> ‘Content-Type’: ‘application/x-www-form-urlencoded’  
> },  
> data: data  
> }).success(function (data, status) {  
> })

But I am **not** stringify the data and I am not setting any proxy. Hope this will help

---

<div class="post-metadata">

**Author:** ![snehalp](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/snehalp/32/92983_2.png) [@snehalp](https://forum.ionicframework.com/u/snehalp)\
**Post date:** [October 27, 2015, 7:55pm UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615/18 "2015-10-27T19:55:30Z")

</div>

Hi Gensollen,

nope tried that, still the same,

Hi Danny,

connecting to your server wont work, my data needs to be on a mysql server and hence i used this API code

> **[mevdschee/php-crud-api](https://github.com/mevdschee/php-crud-api)**
>
> php-crud-api - Single file PHP script that adds a REST API to a SQL database

any one of you know a replacement for this may be?

---

<div class="post-metadata">

**Author:** ![DaDanny](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/dadanny/32/90685_2.png) [@DaDanny](https://forum.ionicframework.com/u/DaDanny)\
**Post date:** [October 28, 2015, 9:10pm UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615/19 "2015-10-28T21:10:54Z")

</div>

I was suggesting hitting my server because I know my server can handle requests from Ionic Serve or an actual device.

Then we could determine if the issue is on the server (not probably handling the requests) or with your POST request.

I could give you one of my POST endpoints, and how I form my request, and you could see how a successful POST request works/looks.

---

<div class="post-metadata">

**Author:** ![snehalp](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/snehalp/32/92983_2.png) [@snehalp](https://forum.ionicframework.com/u/snehalp)\
**Post date:** [October 29, 2015, 2:49pm UTC](https://forum.ionicframework.com/t/http-post-to-php-api-returns-null/35615/20 "2015-10-29T14:49:07Z")

</div>

Thanks Danny for all the help, i tried another hack, created a new php code just for post, its all working fine now, phew…

Thanks again .
