angularjs预检的响应具有无效的HTTP状态代码500

时间:2016-05-05 14:39:50

标签: angularjs cors

我正在从angularjs应用程序向我的api发出一个GET请求,它返回Response for preflight has invalid HTTP status code 500,即使我有CORS设置允许来自任何来源和请求方法。我真正不理解的部分是我也在GET请求之前发出了一个POST请求,它没有返回错误,任何帮助理解这里发生的事情将不胜感激。

请求电话

function getRole (roleId) {
    return $http({
        method: 'GET',
        url: rolesAPIURL + roleId,
        headers: defaultHeaders
    });
}

服务器CORS配置

// Allow from any origin (only for testing)
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, PUT, OPTIONS");
    }

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

0 个答案:

没有答案