Problem with Post ajax and Javascript Object

2

I have the following problem, I have a constructor function that will mount an object for me:

var companyConstructor = function Company(id, logo, name, language, primaryColor, secondaryColor, description, headOffice, serverInfo, serverAddress, publicUrl) {

    if (false === (this instanceof Company)) {
        return new Company();
    }

    this.id = id;
    this.logo = logo;
    this.name = name;
    this.language = language;
    this.primaryColor = primaryColor;
    this.secondaryColor = secondaryColor;
    this.description = description;
    this.headOffice = headOffice;
    this.serverInfo = serverInfo;
    this.serverAddress = serverAddress;
    this.publicUrl = publicUrl;
    this.users = [];
};

And I also have a method in prototype of this function that adds users to the object:

companyConstructor.prototype.add = function (data) {

    var o = {
        id: data.Id,
        name: data.Name,
        email: data.Email                        
    };

    return this.users.push(o);
};

Then I create an instance of this object and in the course of the screen I fill this object:

var company = new companyConstructor();

The problem is that when I give a post passing this object company , it enters the function of add of prototype and an error because it does not find properties.

$.ajax({
    url: '/api/company/',
    dataType: 'json',
    type: 'POST',                
    data: company,
    success: function (data) {                    

    },
    error: function (xhr) {
        console.log(xhr.status);
    }
}); 

Because the add function is inherited I did not think it would be triggered in the post, I think something is wrong, someone could explain it to me.

    
asked by anonymous 27.03.2014 / 19:38

1 answer

1

This is not really a solution, it's another finding. According to the jQuery documentation, when using objects in data they must be simple objects:

  

data
  Type: PlainObject or String
  Data to be sent to the server. It is converted to a query string, if not already the string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key / Value pairs If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

So jQuery will run whatever functions it does to get its result and serialize it. I can not see how you can get around this problem. Ever tried another library?

    
27.03.2014 / 20:50