在Angular.js和bottle.py之间进行通信

时间:2016-01-28 07:52:37

标签: javascript python angularjs bottle

这应该很容易。但显然存在一些问题。所以我想在以下HTML文件中读取瓶子服务器上的一些数据......

<!DOCTYPE html>
<html>
<head>
    <title>Pushing data to the server</title>
    <script type="text/javascript" src='angular.min.js'></script>
    <script type="text/javascript" src='app3.js'></script>
</head>
<body ng-app='myApp'>
    <div ng-controller='friendsController'>
        {{someString}}
        <button ng-click='loadFriends()' > Load Friends </button>
    </div>
</body>
</html>

Javascript是:

var app = angular.module('myApp', []);

app.controller('friendsController', function($scope, $http){
    //code here
    $scope.someString = 'This is some string';

    $scope.loadFriends = function(){
        $http.get('http://localhost:8000/').success(function(data){
            $scope.someString = 'Yay';
        }).error(function(){
            $scope.someString = 'Some error occured';
        });
    };
});

瓶子服务器如下:

import bottle

@bottle.route('/')
def hello(): return {"test":"someData"}

bottle.run(host='localhost', port=8000)

所以一切都很简单!所以现在我测试程序,按下按钮后我总是得到错误'Some error occured'。我尝试了服务器响应的卷曲,这似乎很好......

>curl http://localhost:8000/ -i
HTTP/1.0 200 OK
Date: Thu, 28 Jan 2016 06:37:50 GMT
Server: WSGIServer/0.1 Python/2.7.10
Content-Length: 39
Content-Type: application/json

{"test": "someData"}

我觉得代码中有一些轻微的错误,我无法确定。任何帮助将不胜感激......

N.B。:我已经在Python服务器中尝试了各种类型的输出......

  1. def hello(): return {"test":"someData"}
  2. def hello(): return 'someData'
  3. def hello(): return True
  4. def hello(): return '{"test":"someData"}'
  5. 这些似乎都没有任何区别......

    如果有任何帮助,我总是在Chrome的javascript控制台中收到此错误:

    XMLHttpRequest cannot load http://localhost:8000/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

    显然这意味着数据类型问题。所以我尝试了不同的return语句。

0 个答案:

没有答案