duanma8207 2016-08-12 11:08
浏览 98

请求标头字段在用于PHP API的AngularJS调用中不允许使用Access-Control-Allow-Origin

I am using AngularJS to call php APIs but I have the following problem:

XMLHttpRequest cannot load http://www.example.com/api/v1/getCategories.php. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com' is therefore not allowed access.

AngularJs code:

app.factory("Data", ['$http', 'toaster',
    function ($http, toaster) { // This service connects to our REST API

        var serviceBase = 'http://www.example.com/api/v1/';
        var conf= {headers:  {
            'Access-Control-Allow-Origin':'http://example.com',
            'Content-Type': 'application/json'
         }
        };

        var obj = {};
        obj.toast = function (data) {

            toaster.pop(data.status, "", data.message, 10000, 'trustedHtml');
        }
        obj.get = function (q) {

            return $http.get(serviceBase + q,conf).then(function (results) {

                return results.data;
            });
        };
        obj.post = function (q, object) {
              return $http.post(serviceBase + q, object, conf).then(function (results) {
                return results.data;
            });
        };
        obj.put = function (q, object) {
             return $http.put(serviceBase + q, object).then(function (results) {
                 return results.data;
            });
        };
        obj.delete = function (q) {
            return $http.delete(serviceBase + q).then(function (results) {
                return results.data;
            });
        };
        return obj;
}]);

Can you please help me to figure out what is the problem ?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥15 Python3.5 相关代码写作
    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像