Angular JS中的POST问题

时间:2015-03-15 15:47:57

标签: angularjs rest jetty http-post

我正在尝试使用angular + node j将数据发布到我在jetty中运行的REST服务。 这是代码:

var app = 

    angular.module("loginapp", []);



        app.controller("loginctrl", function($scope,$http) {
                app.config(['$httpProvider', function($httpProvider) {
                $httpProvider.defaults.useXDomain = true;
                delete $httpProvider.defaults.headers.common['X-Requested-With'];
                    }
                ]);
                app.config(['$sceDelegateProvider', function($sceDelegateProvider) {
                    $sceDelegateProvider.resourceUrlWhitelist(['self', 'http://localhost:9011/**']);
                     }]);

                        $scope.login = function(e){
                            console.log('clicked login...');
                            e.preventDefault();             
                            $http({
                                      url: "http://localhost:9011/test/dummy/doStuff1",
                                      method: "POST",
                                      data : {email: $scope.email, password: $scope.password},
                                      headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' },
                                      withCredentials: false,
                                      }).success(function (data, status, headers, config) {
                                            console.log('status',status);
                                            console.log('data',JSON.stringify(data));
                                            console.log('headers',headers);
                                    }); 

                        }

                });

然而,我在Request中传递的数据没有被映射到REST服务中的方法参数,因为没有调用该方法。这是代码:

    @Path("/test/dummy")
public class DummyService {
    @POST
    @Consumes({MediaType.APPLICATION_FORM_URLENCODED})
    @Produces({ MediaType.APPLICATION_JSON})
    @Path("/doStuff1")
    public Response doStuff1(DummyParam param) {
        System.out.println("Hiiiiiiiiii : ");
        return Response.ok().build();
    }

如果没有param参数,则会调用该方法,但是它不起作用。 服务器文件是:

var express = require("express");
var path    = require("path");
var bodyParser = require("body-parser");
var cor = require("cors");
var app     = express();



app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cor());
app.all("/*", function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
    res.header("Access-Control-Allow-Methods", "GET, PUT, POST");
    if (req.method === 'OPTIONS') {
    res.statusCode = 204;
    return res.end();
  } else {
    return next();
  }
});


app.get('/',function(req,res) {
    res.sendFile(__dirname +'/test/test.html')
})
app.listen(3000);

console.log(“在端口3000运行”);   任何人都可以帮我解决这个问题吗?

谢谢

1 个答案:

答案 0 :(得分:0)

如果你看到来自数据的正确,那么它必须是正确设置bodyParser的东西,我有同样的问题并用json文件的bodyParser扩展解决了它。在Express.js中它看起来像这样。

 app.use(bodyParser.json());

我的最后一个猜测是,也许你需要cookie解析器

尝试在body-parser之后添加它:

 app.use(bodyParser.urlencoded({extended: true}));
 app.use(bodyParser.json());
 app.use(cookieParser('SecretCode!'));

我仍然加深了对快递/节点的了解,但是如果不是我建议你在小部件上断开api,这样会更容易看出错误。

相关问题