Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 允许在AngualrJS和PHP中使用COR_Javascript_Php_Angularjs_Xmlhttprequest_Cors - Fatal编程技术网

Javascript 允许在AngualrJS和PHP中使用COR

Javascript 允许在AngualrJS和PHP中使用COR,javascript,php,angularjs,xmlhttprequest,cors,Javascript,Php,Angularjs,Xmlhttprequest,Cors,我将AngualrJS用于带有一些PHP的单页应用程序(MAMP PRO作为开发环境) 在http get请求中,我有第三方api提供程序,它在http请求中返回json 当我发出请求时,我得到“请求的资源上不存在“访问控制允许来源”标题” 基本上我现在在CORS中被困了8个多小时!并且不确定如何让浏览器(Chrome/FF)覆盖CORS并发送http请求 下面是Controller.js中的get请求: 在js中: factory.fetchJson = function (filePath)

我将AngualrJS用于带有一些PHP的单页应用程序(MAMP PRO作为开发环境)

在http get请求中,我有第三方api提供程序,它在http请求中返回json

当我发出请求时,我得到“请求的资源上不存在“访问控制允许来源”标题”

基本上我现在在CORS中被困了8个多小时!并且不确定如何让浏览器(Chrome/FF)覆盖CORS并发送http请求

下面是Controller.js中的get请求:

在js中:

factory.fetchJson = function (filePath) {

        return $http.get(filePath)
            .success(function (data, status) {
                return data;
                console.log('Success Fetch JSON', status);
            }).error(function (data, status, headers, config) {
                console.log('Error Fetch JSON', status);
            });
 }
angualrjs Controller.js中允许CORS的设置

myApp.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    $httpProvider.defaults.withCredentials = true;
    delete $httpProvider.defaults.headers.common["X-Requested-With"];
    $httpProvider.defaults.headers.common["Accept"] = "application/json";
    $httpProvider.defaults.headers.common["Content-Type"] = "application/json";

}
]);
允许index.php标题中的CORS的设置:

    <html>

<?php

    // Allow from any origin
    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);
    }

    echo "You have CORS!";

    ?>


最后,在刚刚安装的chrome中完成所有这些之后: CORS工作正常。

你应该看看这个。