Hello,
I have a Javascript page where there are URLs specified which determines which environment one should use for REST GET requests. Please find the code snippet of how it looks in my javascript page below:
function WebServiceCall(environment_) {
var self = this;
// The environment-specific host/path/URL for the web services.
this.baseURLs = {
dev: /*"https://development-domain.com:8443/myApp/"*/ "proxy.php",
prod: ""
};
this.environment = null;
// web service URLs
this.names = [
"getemployeename"
];
// More code below....
This is throwing an error in my developer tool console (chrome) which is as follows:
Failed to load resource: the server responded with a status of 404 https://development-domain.com/proxy.phpgetemployeename?employee_id=123
I can understand that the above step that I am doing is wrong as I am passing the name of the file in place of the URL. How can I pass control to the proxy php
file so that I can call the webservice from there. I am using proxy to avoid browser related /CORS mechanism related errors.
The contents of my proxy.php
files are as follows:
<?php
// Add an endpoint for every proxied web service.
$data['endpoint']['getemployeename'] = "getemployeename";
/* The User ID is controlled by an external auth system. PHP's handle to the user
* id should be $_SERVER['REMOTE_USER']. If it is not, change the value after the
* equal sign to the php variable that contains the user id.
*/
$data['user_id'] = $_SERVER['REMOTE_USER'];
// Grab the cookie that contains the name of the requested web service.
$service = $_COOKIE["web_service"];
/* Is this a request for the user id of the remote user?
* If so, echo the user id and exit.
*/
if ($service == "get_remote_user") {
echo $data['user_id'];
return;
}
// The URI of the Tomcat server hosting the web services.
// TODO: Make sure it ends with a backslash!
$webServicesURI = "https://development-domain.com:8443/myApp/";
/* Make the URL from the web service URI, the endpoint, and the AJAX call's query string.
* TODO: probably a good idea to raise an exception if the requested
* service doesn't exist in the data array.
*/
$url = $webServicesURI . $data['endpoint'][$service] . '?' . $_SERVER['QUERY_STRING'];
// Open the Curl session.
$session = curl_init($url);
// Don't return HTTP headers. Do return the contents of the call.
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// Make the call, that is, send an http request to the service.
$response = curl_exec($session);
// Set the content type for what is returned from the web service.
header ("Content-Type:text/json");
// Echo whatever the service returned back to the AJAX consumer.
echo $response;
// Clean up the curl session.
curl_close($session);
?>