AngularJS 1.2跨起源请求仅支持HTTP [英] AngularJS 1.2 cross origin requests are only supported for HTTP

查看:151
本文介绍了AngularJS 1.2跨起源请求仅支持HTTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法来配置角度应用使其可用?
即时通讯使用的工厂。

顺便说一句,我使用localhost作为Web服务器,但我正在给其他服务器(同一网络)的请求。

  angular.module('demoApp.factories',[])
    .factory('的DataFactory',['$ HTTP',函数($ HTTP){    VAR urlBase =外部服务器:8080;
    变种的DataFactory = {};
    dataFactory.getTest =功能(){
        返回$ http.get(urlBase +'/测试');
    };    返回的DataFactory;
}]);


解决方案

终于找到了解决办法。将张贴在这里,也许它可以帮助别人:

  angular.module('demoApp.factories',[])
    .factory('的DataFactory',['$ HTTP',函数($ HTTP){    VAR urlBase =外部服务器:8080;
    变种的DataFactory = {};
    dataFactory.getTest =功能(){
        //注意$ HTTP方法改变,并添加一个新的参数(回调)
        //回调参数的值可以是任何
        返回$ http.jsonp(urlBase +'/测试回调= myCallBack函数?');
    };    返回的DataFactory;
}]);

控制器文件基本上只是调用该工厂:

  angular.module('demoApp.controllers',[])。控制器(控制器1',['$范围','的DataFactory',
        功能($范围的DataFactory){    $ scope.status;
    $ scope.data;
    dataFactory.getTest()。成功(功能(数据)
    {
        $ scope.data =数据;
    })错误(功能(错误)
    {
        $ scope.status ='无法加载客户数据:'+返回Error.message;
    });

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;
}]);

解决方案

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;
    });

这篇关于AngularJS 1.2跨起源请求仅支持HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆