0

I'm trying to pass a json object to my factory.login method so I can re use it.

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)
7
  • seems like you have a problem with injecting jquery to your code. are you minimizing your code? Commented Mar 22, 2016 at 13:26
  • well!!!! don't get this wrong....What is the question? Commented Mar 22, 2016 at 13:27
  • No I've not minimized it. Commented Mar 22, 2016 at 13:27
  • also in your html make sure script tag for jquery comes before angularjs Commented Mar 22, 2016 at 13:27
  • Jquery comes first. That's not the problem. Commented Mar 22, 2016 at 13:29

4 Answers 4

2

This should be vm.data

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

And factory doesn't require jQuery at all, just use below construction

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

Sign up to request clarification or add additional context in comments.

Comments

1

Your JSON seems incorrect. Should be:

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

Also the $http.post function is available:

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

Comments

1

JSON is a format to serialize object, so it's a string. 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. You don't need to do that serialization your self.

Documentation https://docs.angularjs.org/api/ng/service/$http -> Default Transformations ->

Request transformations ($httpProvider.defaults.transformRequest and $http.defaults.transformRequest):

If the data property of the request configuration object contains an object, serialize it into JSON format. Response transformations ($httpProvider.defaults.transformResponse and $http.defaults.transformResponse):

If XSRF prefix is detected, strip it (see Security Considerations section below). If JSON response is detected, deserialize it using a JSON parser.

The stack trace show the following error :

TypeError: Cannot read property 'jquery' of undefined

Comments

1

miss match variable uses declared var data = {} but used vm.data in your controller. should declare as

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

or

use data in employeeFactory.login(vm.url, data) if declare as var data= {}

and in your factory no need to use $.param can send as argument in post method like

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

Angular Documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.