Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I copied and pasted this code directly from somewhere else that worked perfectly I just uploaded to my own server on azure and I get no response but there's definitely a request going in. I have no idea how it's not responding when it's the exact same code! Any ideas?

<?php
if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400');    // cache for 1 day
}

// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
        header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

    exit(0);
}


$postdata = file_get_contents("php://input");
if (isset($postdata)) {
    $request = json_decode($postdata);
    $username = $request->username;

    if ($username != "") {
        echo "Server returns: " . $username;
    }
    else {
        echo "Empty username parameter!";
    }
}
else {
    echo "Not called properly with username parameter!";
}
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
154 views
Welcome To Ask or Share your Answers For Others

1 Answer

As by default, when you create an Azure Web App service in PHP, there is not variable named HTTP_ORIGIN in PHP runtime, and the variable REQUEST_METHOD is only set as 'GET. You can write code phpinfo(); in your PHP script on Azure to check the configurations in PHP runtime on Azure.

So the code in the first 2 IF closures will not ever run. So if your AngularJs client is not in the same domain with your PHP server (they should be in one Azure Web App Service here), when your client request the PHP server, it will raise a CORS issue.

So you can set your custom HTTP_ORIGIN in app settings on Azure portal, which will be set in PHP runtime the your application start.

  • Login Azure classic portal, header to the page to manage your website.
  • Click the CONFIGURE tab, scroll down to the app settings section
  • Set the HTTP_ORIGIN for your AngularJs client endpoint. E.G. If your AngularJs client runs on local, you should set the HTTP_ORIGIN as localhost: enter image description here
  • Click save button at bottom nav and click restart button to restart the website to load your custom settings.

Then you can test your AngularJs application on your local server:

$http({
    url: 'http://<your_web_site_na,e>.azurewebsites.net/<post_page>.php',
    method: "POST",
    data: { username: 'username', password: 'password' },
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  })
  .success(function (data, status, headers, config) {
    console.log(data);
  })
  .error(function (data, status, headers, config) {
    $scope.status = status;
  });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...