2

I need to pass an object within the data object but it's not working

I use the following function I found on this very site, very useful, to convert form data to JSON

$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

and then I need to pass the object as a sub-object, but it's not working. Filters isn't even showing up in the query string parameters in the inspector

var filters = $('.filtersForm').serializeObject();

$.ajax({
    type: 'GET',
    data: {script:'search',page:page,filters:filters},
    success: function(data){
    }
});

enter image description here

See how "filters" is missing in the picture

Can someone please explain why I can't pass an object like that?

1
  • 1
    Any particular reason you're using this serializeObject function instead of JSON.stringify()? Commented Jul 11, 2013 at 17:58

2 Answers 2

4

Try this instead:

$.ajax({
  type: 'POST',
  data: JSON.stringify({
    script: 'search',
    page: page,
    filters: filters
  }),
  contentType: 'application/json'
});
  1. Changed type from GET to POST. This will allow you to send a request body.
  2. Stringify your data parameter using the built-in JSON object to stringify your JS object to a JSON-formatted string. (older browsers may not have this built-in object, in that case, add it using json2.js)
  3. Set contentType to application/json. This basically states that the request-body is of this type... which it is because we just stringified it to JSON.
Sign up to request clarification or add additional context in comments.

5 Comments

Why would I need to stringify my initial object?
Serializing your JavaScript object to a JSON-formatted string is the best way to send your complex data to the server (part opinion).
Well but sending objects works as well, why can't I send an object within the object without stringyfiying ? I updated the question with a picture
Data sent over HTTP has to be flat (serialized). JSON is often used because it lets you serialize complex objects. You can then easily deserialize on the server. If you 'stringify' your filters object, it should go over the wire with no problem.
Well that worked as to getting the parameter to show up, but now the object is always empty.. Doh!
0

Try to specifiy your filters
For example in try : jsonfilter = JSON.stringify(filters);
If youre using MVC and ASP.Net you can try In your HTTPWebMethode which have a jsonResult as Return you can try to specifiy parameters

  public JsonResult myPostMethode(string script,object page,List<object> filters){

    //make some usefull 
    var model = script;
    return(model,JsonRequestBehavior.AllowGet);
}

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.