stoshb
stoshb

Reputation: 65

Need example of cross origin request from angular js

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.

Upvotes: 0

Views: 24

Answers (1)

stoshb
stoshb

Reputation: 65

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  );
    }   

?>

Upvotes: 1

Related Questions