在AngularJS中使用POST与Google App Engine Python服务器

0 投票
2 回答
2067 浏览
提问于 2025-04-17 19:43

我遇到一个问题。

我一直在用 $resource 来做简单的 GET 请求,但最近我需要发送大量数据到服务器,结果出现了 414 错误,提示 URI 太大。我知道解决这个问题的方法之一是用 POST 请求代替 GET 请求。

我试着找一些解决方案,比如使用 $http.post 并将参数传递给 $http,但都没有成功。有人能给我一个教程,教我该怎么做吗?

编辑:

这是我已经做的:

    var data_2 = $.param({'method':"update_proj",
            'proj_id': proj_id,
            'ptitle': project.title,
            'pdescription': p_desc,
            'pexposure': tech,
            'pcompany': project.company,
            'pteamsize':project.teamsize,
            'ppoc': project.poc,
            'pemail': c_email,
            'pcontact': project.contact,
            'pimg':project.img,
            'pvideo':project.video,
            'prskill': rskills,
            'poutcome': project.outcome});

    $http({method:'update_proj',
      url: "http://osmosisgal.appspot.com/update_proj", 
      data: data_2,
      headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
    .success(function(data,status){
     ...
    });

这是我在 GAE 上的 Python 代码:

    class ActionHandler(webapp.RequestHandler):
       def update_project(self):
           proj_id = self.request.POST["proj_id"]
               .
               .
               .

但我还是遇到了这个错误

Traceback (most recent call last):
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~osmosisgal/1.366101252356646323/new_backend.py", line 1274, in update_project
    proj_id = self.request.POST["proj_id"]
  File "/python27_runtime/python27_lib/versions/third_party/webob-1.1.1/webob/multidict.py", line 312, in __getitem__
    return self._decode_value(self.multi.__getitem__(self._encode_key(key)))
  File "/python27_runtime/python27_lib/versions/third_party/webob-1.1.1/webob/multidict.py", line 568, in __getitem__
    raise KeyError("No key %r: %s" % (key, self.reason))
KeyError: "No key 'proj_id': Not a form request"

2 个回答

0

我解决这个问题了。最开始我没有把所有的静态文件放在GAE上,后来我把它们都放上去了,结果一切都正常了。

1

文档中可以看到,这个方法首先获取的是方法的类型,而不是网址。

$http({method: 'POST', url: '/someUrl', data: my_data}).
  success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
  }).
  error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

对于你的数据,你可以创建一个包含键值对的json对象,然后像上面那样传递给函数。

另外,不要忘记在你的控制器定义中传入$http。

function MyController($scope, $http) {...}; 

撰写回答