简体   繁体   English

AngularJs在帖子中使用对象

[英]AngularJs use object in post

I'm trying to pass a json object to my factory.login method so I can re use it. 我正在尝试将json对象传递给factory.login方法,以便我可以重新使用它。

This is my code: 这是我的代码:

Controller function 控制器功能

var data = {email:'test','password':'test'};

vm.login = function() {
            employeeFactory.login(vm.url, vm.data)
                    .then(function(response) {
                        console.log(response);
                    }, function(data)
                    {
                        console.log(data.status);
                    });
        }

Factory

factory.login = function(url,data) {
        return $http({
            'method': 'POST',
            'url': url,
            'data': $.param(
                data
            ),
            'headers': {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        });
    }
    return factory;

But the error is: 但是错误是:

angular.js:13294 TypeError: Cannot read property 'jquery' of undefined
    at Function.n.param (jquery-2.2.2.min.js:4)
    at Object.factory.login (employeeFactory.js:14)
    at employeeController.vm.login (employeeController.js:16)
    at fn (eval at <anonymous> (angular.js:14138), <anonymous>:4:285)
    at b (angular.js:15151)
    at e (angular.js:24674)
    at m.$eval (angular.js:16895)
    at m.$apply (angular.js:16995)
    at HTMLButtonElement.<anonymous> (angular.js:24679)
    at HTMLButtonElement.n.event.dispatch (jquery-2.2.2.min.js:3)

This should be vm.data 这应该是vm.data

vm.data = {email:'test','password':'test'};

And factory doesn't require jQuery at all, just use below construction 而且工厂根本不需要jQuery,仅在构造以下使用

factory.login = function(url,data) {
    return $http({
        'method': 'POST',
        'url': url,
         //don't use $.param, as you need jQuery dependency for that
         //for x-www-form-urlencoded you have to use this construction
         //email=test&password=test
        'data': 'email=' + data.email + '&password=' + data.password, 
        'headers': {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });
}
return factory;

But consider using JSON type on server request handler, as it's much easier 但是考虑在服务器请求处理程序上使用JSON类型,因为它更容易

Your JSON seems incorrect. 您的JSON似乎不正确。 Should be: 应该:

var data = { "email": "test", "password": "test"};

Also the $http.post function is available: $http.post函数也可用:

$http.post(url, data, [{headers: { 'Content-Type' : 'application/x-www-form-urlencoded'}]);

JSON is a format to serialize object, so it's a string. JSON是序列化对象的格式,因此它是一个字符串。 You have your object as data that contains the data to be sent to the server, so just do this: 您将对象作为包含要发送到服务器的数据的数据,因此只需执行以下操作:

Just put your data object in it: 只需将数据对象放入其中:

factory.login = function(url,data) {
    return $http({
        'method': 'POST',
        'url': url,
        'data': data
    });
}
return factory;

Angular will send json to the server in the payload. Angular将在有效负载中将json发送到服务器。 You don't need to do that serialization your self. 您不需要自己进行序列化。

Documentation https://docs.angularjs.org/api/ng/service/ $http -> Default Transformations -> 文档https://docs.angularjs.org/api/ng/service/ $ http->默认转换->

Request transformations ($httpProvider.defaults.transformRequest and $http.defaults.transformRequest): 请求转换($ httpProvider.defaults.transformRequest和$ http.defaults.transformRequest):

If the data property of the request configuration object contains an object, serialize it into JSON format. 如果请求配置对象的data属性包含一个对象,则将其序列化为JSON格式。 Response transformations ($httpProvider.defaults.transformResponse and $http.defaults.transformResponse): 响应转换($ httpProvider.defaults.transformResponse和$ http.defaults.transformResponse):

If XSRF prefix is detected, strip it (see Security Considerations section below). 如果检测到XSRF前缀,则将其剥离(请参阅下面的“安全注意事项”部分)。 If JSON response is detected, deserialize it using a JSON parser. 如果检测到JSON响应,请使用JSON解析器反序列化。

The stack trace show the following error : 堆栈跟踪显示以下错误:

TypeError: Cannot read property 'jquery' of undefined

miss match variable uses declared var data = {} but used vm.data in your controller. miss match变量使用声明的var data = {}但在控制器中使用了vm.data should declare as 应该声明为

vm.data= {email:'test','password':'test'}

or 要么

use data in employeeFactory.login(vm.url, data) if declare as var data= {} 使用dataemployeeFactory.login(vm.url, data)如果声明为var data= {}

and in your factory no need to use $.param can send as argument in post method like 并且在您的工厂中不需要使用$.param可以在post方法中作为参数发送,例如

$http.post(url, data, [{headers: { 'Content-Type' : 'application/x-www-form-urlencoded'}]);

Angular Documentation 角度文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM