HTTP Post to php api returns null


i’m using this api on my server here , 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
using 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.


1 Like

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 ?

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 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.

Hi gensollen,

here is the post function

$scope.doSubmit = function(addteam){
var url = “/teams”;
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
error(function () {
// if you are here something is not going so good

Hi Danny,

tried , returns the following…

with the following parameters

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


So in my config.js file, which is basically


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.


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 :


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)

You want to call

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

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.

Hi Danny,

1)added the config, no change
2)server is not local, its here the api code sourced from here, 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 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’ is therefore not allowed access. The response had HTTP status code 404.

Thanks a lot for all the help thus far

No problem, I love helping :slight_smile:

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.

1 Like

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

Ionic View , returns null a well

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.


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.

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?

Hmmm… I’m starting to feel your frustration :wink: 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.



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

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

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

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

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.

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 .