如何从 angular 应用调用 webapp post url

How to call webapp post url from angular app

这是我的网络应用 Class

class CreateChannel(webapp.RequestHandler):
    def post(self):
        userEmail = self.request.get("userEmail", default_value='')
        userDBId = self.request.get("userId", default_value='')
        logging.info("Deleting existing channel and creating new channle for User Id : " + userDBId)

我正在尝试从 angular 控制器

var objUser = JSON.stringify({
            "userId": Session.activeUserId,
            "email": Session.userId
        });

var deferred = $q.defer();
var route = URLS.AppUrl + "/createchannel";
deferred.resolve($http({
            method: 'POST',
            url: route,
            data: objUser
        }).success(function (response) {
            console.log("GeneralService : Channel Created");
        }).error(function(err){
            console.log("error")
        }));

我无法获取传递的参数。有什么问题?

你能试试这样吗:

var objUser = {
        "userId": Session.activeUserId,
        "userEmail": Session.userId
    };

var deferred = $q.defer();
var route = URLS.AppUrl + "/createchannel";
deferred.resolve($http({
            method: 'POST',
            url: route,
            data: objUser
        }).success(function (response) {
            console.log("GeneralService : Channel Created");
        }).error(function(err){
            console.log("error")
        }));

您的服务器支持其他格式,因此您必须更改您的 objUser。

对于jsonContent-Type,你应该这样做,

import json
class CreateChannel(webapp.RequestHandler):
    def post(self):
        params = json.loads(self.request.body)
        userEmail = params.get("userEmail") if params.get("userEmail") else ""
        userDBId = params.get("userId") if params.get("userId") else ""