0

Have a question that I hope can be answered simply.

I have an application on server xxx.yyy.com written in angular.js. It accesses data, via HTTP/JSONP requests, on server abc.def.com.

I want allow my data server to support POST requests from other servers.

In developing this capability, I want to prove out the process where I replace the JSONP request with a POST request. Am now getting CORS errors. I understand what is happening here but I can not figure out a way fix the problem.

In this sample code, on xxx.yyy.com I make this request:

kaunasAPI.simPost = function(meter_data)
    {
    URL = 'http://abc.def.com/models/ajx_reading.php';
    return $http({
    method: 'POST',
    data: meter_data,
    url: URL
        });
    }

On the server abc.def.com I have the following code:

<?php

    $jTableResult = array();
    $jTableResult['Result'] = "OK";
    $json = json_encode($jTableResult);
    exit ( $json  );
    }   

?>

What am I missing to make this work? Thanks in advance.

stoshb
  • 65
  • 1
  • 11
  • Possible duplicate of [How do CORS and Access-Control-Allow-Headers work?](https://stackoverflow.com/questions/12630231/how-do-cors-and-access-control-allow-headers-work) – Jasper Seinhorst Nov 14 '17 at 14:47

1 Answers1

1

Turns out I have been looking for answer on the wrong side, i.e. angular side.

Answer was to add a line to the server code. and the code is now:

<?php
        {
    header("Access-Control-Allow-Origin: *");       //Fixes cross origin issue
    $jTableResult = array();
    $jTableResult['Result'] = "OK";
    $json = json_encode($jTableResult);
    exit ( $json  );
    }   

?>
stoshb
  • 65
  • 1
  • 11