Angularjs - requesting PHP script from own server returns 403

坚强是说给别人听的谎言 提交于 2019-12-12 01:48:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!