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
2 Answers
Reset to default 4Try this instead:
$.ajax({
type: 'POST',
data: JSON.stringify({
script: 'search',
page: page,
filters: filters
}),
contentType: 'application/json'
});
- Changed type from
GET
toPOST
. This will allow you to send a request body. - 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)
- 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);
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744754837a4591817.html
评论列表(0条)