jQuery and my passed data to ajax is not at all what I intend
-
30-07-2021 - |
Pergunta
I am not sure what is going on. I've done exhausted searching for an answer. I thought it was processData but the ajax won't fire off in that case. I can't for the life of me figure out what is going on.
I am trying to send data via POST, ajax.
Because of how my data is set up, in page, I can actually create a hash and push it into an array. So I have something like.
The data I am forming looks like this:
in my init func, I set up some vars.
this.dataHolder = [];
in a method in my plugin:
_acceptData:function(){
var item = {};
item[this.schema.itemId] =
{
"Color" : this.schema.color,
"Size" : this.schema.size,
"Cost" : this.schema.cost
}
return item;
},
basically I have some other methods and such, but the jist is that that data structure above gets put into: this.dataHolder
when I dump the data, looks like it should. Various console.logs thruout the code shows me the data is well formed etc.. BUT when I pass it into the ajax data.. it does wonky things.
When I have my ajax.. ala:
var self = this;
.ajax({
type: "POST",
data : {matchedItems : self.dataHolder},
dataType: json,
url : /some/path/
I tried soo many different things, and in all cases the data is either undefined (when I Post it) OR its in a weird format, like: (in logs)
$VAR1 = {
'matchedItems[0][itemid1][Color]' => 'Blue',
'matchedItems[0][itemid1][Size]' => '11',
'matchedItems[0][itemid1][Cost]' => '33.90',
'matchedItems[1][itemid2][Color][]' => 'Silver',
'matchedItems[1][itemid2][Size]' => '13',
'matchedItems[1][itemid2][Cost]' => '44',
'matchedItems[2][itemid3][Color][]' => 'Red',
'matchedItems[2][itemid3][Size]' => '9',
'matchedItems[2][itemid3][Cost]' => '23'
};
or different permutations
data : MatchedItems : {self.dataHolder}
or
data : {MatchedItems : {self.dataHolder}}
etc.. doesn't matter, the POST always gets some really odd formatting.
This is the way it should look when I send it and how they receive it.
matchedIeds = {
itemId1 :
{
Color : Red,
Size : 11,
Cost : 44
},
itemId2 :
{
Color : Blue,
Size : 8,
Cost : 12
},
itemId3 :
{
Color : Blue,
Size : 19,
Cost : 88.09
}
}
I mean, I am setting up my data like that so I can't see what is wrong.
Note: I tried processData : false, but that didn't help at all.
EDIT: Here is the code console.log'd out write before I pass it into the .ajax method. Console.log
[Object { itemid1={...}}, Object { itemid2={...}}, Object { itemid3={...}}]
Here it is in the params of the post: jquery.js (line 641) POST http://www.myurl/to/test
200 OK
79ms
jquery.js (line 7829)
HeadersPostResponseJSONCookies
Parametersapplication/x-www-form-urlencoded
undefined undefined
undefined undefined
undefined undefined
Nenhuma solução correta