Pass js object with ajax request
source link: https://www.codesd.com/item/pass-js-object-with-ajax-request.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Pass js object with ajax request
This question already has an answer here:
- jQuery posting valid json in request body 1 answer
I need to send a js object to the server through ajax request; is an object containing parameters for a sql query with Sequelize orm in node js; an example is like this:
var data =
{
include: [
{ model: model.Shop },
{ model: model.Product,
include: [
{ model: model.File }
]
}
]
}
It can contain arrays of objects nested on multiple levels; before sending it I can convert it to valid JSON if needed, like this:
var data =
{
"include": [
{ "model": "model.Shop" },
{ "model": "model.Product",
"include": [
{ "model": "model.File" }
]
}
]
}
I've tried to send it as JSON:
$.ajax({
//...
data: data
});
The problem is that when in the node server I do JSON.parse of the received string, the value of each property is a string and it is not recognized as a model object;
How can I make my server able to understand this?
Try to use JSON.stringify
$.ajax({
data: JSON.stringify(data)
});
Related Articles
How to POST an array of objects with $ .ajax (jQuery or Zepto)
What is this type of javascript? And help with AJAX request
Pass javascript objects with parentheses as PHP keys
How to send a JS object with ajax
Difficulties with ajax request
Using Authentication with Ajax.Request
Send javascript object with AJAX
Race condition related to Autoload in cucumber with AJAX requests
Passing several parameters with $ .ajax url
Laravel - I succeed the beginning and the end of the price range with ajax request but I can not come back the right way
How do you deal with AJAX requests when the user session expires or when the request ends with a 302
How can I pass more parameters with AJAX
How to save a session on a remote server with ajax request javascript / PHP
Pass the object with the object method to work
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK