Figuring out proper way to pass control to the PHP file from javascript and get the response back


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: /*""*/ "proxy.php",
        prod: ""

    this.environment = null;

    // web service URLs 
    this.names = [


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

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:


// 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'];

// The URI of the Tomcat server hosting the web services. 
// TODO: Make sure it ends with a backslash!
$webServicesURI = "";

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


It seems to me that you’re not actually calling “proxy.php” - before you are calling it, something is appending “getemployeename”, and then appending the parameter name and value to the end of that. Because there’s no delimiter after the proxy.php section of that call, you’re getting a 404 (page not found) error when it tries to open “proxy.phpgetemployeename” which doesn’t seem surprising.

So whatever code is actually calling the proxy php code needs to pass “getemployeename” as another parameter.

@droopsnoot Thanks for your answer. Actually there is nothing which is calling the php file right now. I am trying to figure out a way so that I can call the php file. Do you think doing something like this will make sense?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.