angularjs使用$ http.get调用外部URL

时间:2016-01-27 08:45:53

标签: angularjs json

我有一个角度问题和调用外部json,事实是本地工作完美,但当我用一个完整的URL打电话给我404,我留下代码,以防你看到丢失的东西,谢谢:

for thing, thingBack in zip(things, thingsBack):
    thingBack[1] = inputFloat // thing
    # etc.

添加index.html的代码

// JavaScript Document
var angularTodo = angular.module('lostsysApp', []);

function mainController($scope, $http) {
    $scope.names = [];

    $http.get('http://www.viudadesoubrier.com/angular/model.php')
        .success(function(data) {
            $scope.names = eval(data);
            console.log(data)
        })
        .error(function(data) {
            alert(data);
            console.log('Error: ' + data);
        });

    $scope.addNom = function() {
        $http.post('http://www.viudadesoubrier.com/angular/model.php', { op: 'append', nom: $scope.nom, telefon: $scope.telefon } )
            .success(function(data) {
                $scope.names = eval(data);
                console.log(data)
            })
            .error(function(data) {
                console.log('Error: ' + data);
            });

        $scope.nom="";
        $scope.telefon="";
    }

    $scope.delNom = function( nom ) {
        if ( confirm("Seguro?") ) {
            $http.post('http://www.viudadesoubrier.com/angular/model.php', { op: 'delete', nom: nom } )
                .success(function(data) {
                    $scope.names = eval(data);
                    console.log(data)
                })
                .error(function(data) {
                    console.log('Error: ' + data);
                });
        }
    }
}

谢谢。

1 个答案:

答案 0 :(得分:7)

控制器:

var angularTodo = angular.module('lostsysApp', []);    


        angularTodo.controller('mainController', function($scope, $http) {
            $scope.names = [];

            $http.get('http://www.viudadesoubrier.com/angular/model.php')
                .success(function(data) {
                    $scope.names = eval(data);
                    console.log(data)
                })
                .error(function(data) {
                    alert(data);
                    console.log('Error: ' + data);
                });

            $scope.addNom = function() {
                $http.post('http://www.viudadesoubrier.com/angular/model.php', { op: 'append', nom: $scope.nom, telefon: $scope.telefon } )
                    .success(function(data) {
                        $scope.names = eval(data);
                        console.log(data)
                    })
                    .error(function(data) {
                        console.log('Error: ' + data);
                    });

                $scope.nom="";
                $scope.telefon="";
            }

            $scope.delNom = function( nom ) {
                if ( confirm("Seguro?") ) {
                    $http.post('http://www.viudadesoubrier.com/angular/model.php', { op: 'delete', nom: nom } )
                        .success(function(data) {
                            $scope.names = eval(data);
                            console.log(data)
                        })
                        .error(function(data) {
                            console.log('Error: ' + data);
                        });
                }
            }
        });

从服务器端启用CORS

Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Origin: *