问题
I'm using AngularJS for my website. I wan't to make a GET request to my mysql database using php. I've created a simple php script
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$conn = new mysqli($server, $username, $password, $database);
mysqli_set_charset($conn, "utf8");
$result = $conn->query("SELECT name FROM table");
$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
if ($outp != "") {
$outp .= ",";
}
$outp .= '{"name":"' . $rs["name"] . '",';
}
$outp ='{"records":['.$outp.']}';
$conn->close();
echo($outp);
?>
when I request this php file with angularJs http.get the response is 403 forbidden. My AngularJS code:
getScript: function () {
var req = {
method: 'GET',
url: '/app/scripts/script.php',
headers: {
'Content-Type': 'application/json'
}
};
return $http(req);
}
As you can see I get 403 even tho it's from same server. What am I missing here?
回答1:
getScript: function () {
var req = {
method: 'GET',
url: '/app/scripts/script.php',
};
return $http(req);
}
Try not to customize the Request header. This Should solve the 403 Forbidden Issue.
回答2:
Alright I figured it out. I got 403 due to this line in my .htaccess file:
Options FollowSymLinks
来源:https://stackoverflow.com/questions/43593433/angularjs-requesting-php-script-from-own-server-returns-403