Problem with Post ajax and Javascript Object

Asked

Viewed 275 times

2

I have the following problem, I have a construction function that will assemble 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 adding 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 by passing this object company, he enters the function of add of prototype and makes a mistake because it does not find the properties.

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

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

Because of the function add inherited not thought she would be shot in the post, I think something is wrong, someone could explain me.

  • Uncaught TypeError: Cannot read property 'Id' of undefined He is unable to find the property Id that is within the function .add in the object prototype, it is this error that is giving in the console.

1 answer

1


This is not exactly a solution, it’s more of a finding. According to jQuery’s documentation, when using objects in data they must be simple objects:

date
Type: Plainobject or String
Data to be sent to the server. It is converted to a query string, if not already a 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).

The bold part says: Objects must be key pairs/value

So jQuery will run all the functions to get its result and serialize it. I don’t see how you can turn that problem around. Have you tried another library?

  • I get it, so I think I’m going to modify the shape it’s made to ultimately have a simple object, no inheritance with the prototype.

  • @fbadaro, I think it’s best as well. By the way, you can try it using Mootools. The code would be: http://jsfiddle.net/gtkZL/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.