I'm new in REST API. I'm building up a test environment and trying to make a handshake between Rest API and Client side PHP.
My JSON data is at
the data is like:
[
{"Truck_ID":1,"Company":"Food Mall","Distance":2000},
{"Truck_ID":2,"Company":"Food Mall","Distance":4000},
{"Truck_ID":3,"Company":"Food Mall","Distance":3050}
]
I'm trying to act like a client, use PHP to get the data, and put the data in an array. What I tried on client side which is http://localhost:8080 is
<?php
$url = "http://localhost:50417/api/device";
$response = file_get_contents($url);
echo $response;
?>
I also tried js like
<script type="text/javascript">
$(document).ready(function(){
$.getJSON("http://localhost:50417/api/device",
function(data){
alert (data) // this will show your actual json array
});
});
</script>
But no matter what I tried, I always get a "No 'Access-Control-Allow-Origin' header is present on the requested resource." What could be the problem? How clients generally get the data from server using REST API? Thank you.
http://localhost:50417/api/devicein a browser? JavaScript may give you aAccess-Control-Allow-Originerror, but PHP'sfile_get_contentsshouldn't. What error do you get with the PHP code?