最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery ajax: object in data object - Stack Overflow

programmeradmin0浏览0评论

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){
    }
});

See how "filters" is missing in the picture

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

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){
    }
});

See how "filters" is missing in the picture

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

Share Improve this question edited Mar 24, 2014 at 14:05 php_nub_qq asked Jul 11, 2013 at 17:53 php_nub_qqphp_nub_qq 16.1k22 gold badges81 silver badges149 bronze badges 1
  • 1 Any particular reason you're using this serializeObject function instead of JSON.stringify()? – Jon Commented Jul 11, 2013 at 17:58
Add a ment  | 

2 Answers 2

Reset to default 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.

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);
}
发布评论

评论列表(0)

  1. 暂无评论