return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - AngularJS 1.2 cross origin requests are only supported for HTTP - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS 1.2 cross origin requests are only supported for HTTP - Stack Overflow

programmeradmin0浏览0评论

Is there any way to configure Angular app to make it available? Im using a factory.

By the way, I'm using localhost as webserver but I'm making a request to other server (same network).

angular.module('demoApp.factories', [])
    .factory('dataFactory', ['$http', function($http) {

    var urlBase = 'external-server:8080';
    var dataFactory = {};
    dataFactory.getTest = function () {
        return $http.get(urlBase + '/test');
    };

    return dataFactory;
}]);

Is there any way to configure Angular app to make it available? Im using a factory.

By the way, I'm using localhost as webserver but I'm making a request to other server (same network).

angular.module('demoApp.factories', [])
    .factory('dataFactory', ['$http', function($http) {

    var urlBase = 'external-server:8080';
    var dataFactory = {};
    dataFactory.getTest = function () {
        return $http.get(urlBase + '/test');
    };

    return dataFactory;
}]);
Share Improve this question asked Sep 24, 2013 at 19:16 user1214120user1214120 1731 gold badge2 silver badges15 bronze badges 3
  • Maybe if I use jQuery for this part instead? – user1214120 Commented Sep 24, 2013 at 19:26
  • 1 stackoverflow./questions/16661032/… – Jason Commented Sep 24, 2013 at 22:58
  • possible duplicate of AngularJS Error: Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https – Chirag Commented May 27, 2015 at 2:01
Add a ment  | 

2 Answers 2

Reset to default 4

Finally found the solution. Will post here, maybe it can help others:

angular.module('demoApp.factories', [])
    .factory('dataFactory', ['$http', function($http) {

    var urlBase = 'external-server:8080';
    var dataFactory = {};
    dataFactory.getTest = function () {
        //Note the $http method changed and a new parameter is added (callback)
        //the value of the callback parameter can be anything
        return $http.jsonp(urlBase + '/test?callback=myCallback');
    };

    return dataFactory;
}]);

The controller file basically just calling this factory:

angular.module('demoApp.controllers', []).controller('controller1', ['$scope', 'dataFactory', 
        function ($scope, dataFactory) {

    $scope.status;
    $scope.data;
    dataFactory.getTest().success(function (data) 
    {
        $scope.data = data;
    }).error(function (error) 
    {
        $scope.status = 'Unable to load customer data: ' + error.message;
    });

You may be looking to configure the $httpProvider as specified here: http://better-inter/enabling-cors-in-angular-js/

Another way is to create a simple http proxy on your front-end server to make external requests look like to angular that they're ing from the same server.

发布评论

评论列表(0)

  1. 暂无评论