Error while sending array in array using AJAX











up vote
0
down vote

favorite












I am trying to pass an array in an array via ajax. If I do not pass in the additional array, it works just fine.



For example:



var settings = ;
// add stuff to the array

$.ajax({
type: 'POST',
url: "api/update-settings",
data: {
userId: 1,
userSettings: settings
},
done: function(response) {
//do something with the response
},
fail: function() {
// do error stuff
}
});


Sending this will not work. The API (in PHP) gets the code and is able to tell me what userId is, but userSettings is not defined.




Notice: Undefined index: userSettings in /api/update-settings.php on
line 9




However, if I set the settings variable as a different data type (such as an int or string), the index is no longer undefined.



Within the PHP, when I dump request to see what's in it, userSettings is not found:



var_dump($_REQUEST);
Output: array(1) { ["userId"]=> string(1) "1" }


I'm adding items to my array using settings['template'] = template;



Before I submit the ajax request, I can console log it and I get this:



[p: "setting1", s: 1587, emp: "setting2", ems: 3245, template: "", …]


which contains all data that I need.



I tried to stringify the data with JSON.stringify(settings) but then ajax passes an empty array:



array(2) { ["userId"]=> string(1) "1" ["userSettings"]=> string(2) "" } 


I feel like this is something simple, but I can't figure it out. And yes, I've done my searches - I've found similar things, but none of the responses seem to help.



Thank you, in advance.










share|improve this question




















  • 1




    Can you verify that you are actually sending all of the data via the network tab? Check this out stackoverflow.com/a/21617685/2191572 and find your "Request Headers". It's also possible that you are having cache issues or you are testing the completely wrong page.
    – MonkeyZeus
    Nov 8 at 19:32








  • 2




    Are you sure you're actually "adding stuff to the (settings) array"? Because if you're not, I'm pretty sure it won't send anything for an empty userSettings array in the request's body.
    – Jeto
    Nov 8 at 19:38












  • @MonkeyZeus - Well, if I change the variable to NOT be an array, it sends, so I am assuming it's sending.
    – Apolymoxic
    Nov 8 at 19:53










  • @Jeto Yes, I am sure. If I do a console.log I get the full array filled with the correct data.
    – Apolymoxic
    Nov 8 at 19:54






  • 1




    @Jeto is correct anyways, jQuery will not send an empty array like that. You can verify by doing var settings = [ 'yay something!' ]; instead
    – MonkeyZeus
    Nov 8 at 20:01

















up vote
0
down vote

favorite












I am trying to pass an array in an array via ajax. If I do not pass in the additional array, it works just fine.



For example:



var settings = ;
// add stuff to the array

$.ajax({
type: 'POST',
url: "api/update-settings",
data: {
userId: 1,
userSettings: settings
},
done: function(response) {
//do something with the response
},
fail: function() {
// do error stuff
}
});


Sending this will not work. The API (in PHP) gets the code and is able to tell me what userId is, but userSettings is not defined.




Notice: Undefined index: userSettings in /api/update-settings.php on
line 9




However, if I set the settings variable as a different data type (such as an int or string), the index is no longer undefined.



Within the PHP, when I dump request to see what's in it, userSettings is not found:



var_dump($_REQUEST);
Output: array(1) { ["userId"]=> string(1) "1" }


I'm adding items to my array using settings['template'] = template;



Before I submit the ajax request, I can console log it and I get this:



[p: "setting1", s: 1587, emp: "setting2", ems: 3245, template: "", …]


which contains all data that I need.



I tried to stringify the data with JSON.stringify(settings) but then ajax passes an empty array:



array(2) { ["userId"]=> string(1) "1" ["userSettings"]=> string(2) "" } 


I feel like this is something simple, but I can't figure it out. And yes, I've done my searches - I've found similar things, but none of the responses seem to help.



Thank you, in advance.










share|improve this question




















  • 1




    Can you verify that you are actually sending all of the data via the network tab? Check this out stackoverflow.com/a/21617685/2191572 and find your "Request Headers". It's also possible that you are having cache issues or you are testing the completely wrong page.
    – MonkeyZeus
    Nov 8 at 19:32








  • 2




    Are you sure you're actually "adding stuff to the (settings) array"? Because if you're not, I'm pretty sure it won't send anything for an empty userSettings array in the request's body.
    – Jeto
    Nov 8 at 19:38












  • @MonkeyZeus - Well, if I change the variable to NOT be an array, it sends, so I am assuming it's sending.
    – Apolymoxic
    Nov 8 at 19:53










  • @Jeto Yes, I am sure. If I do a console.log I get the full array filled with the correct data.
    – Apolymoxic
    Nov 8 at 19:54






  • 1




    @Jeto is correct anyways, jQuery will not send an empty array like that. You can verify by doing var settings = [ 'yay something!' ]; instead
    – MonkeyZeus
    Nov 8 at 20:01















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to pass an array in an array via ajax. If I do not pass in the additional array, it works just fine.



For example:



var settings = ;
// add stuff to the array

$.ajax({
type: 'POST',
url: "api/update-settings",
data: {
userId: 1,
userSettings: settings
},
done: function(response) {
//do something with the response
},
fail: function() {
// do error stuff
}
});


Sending this will not work. The API (in PHP) gets the code and is able to tell me what userId is, but userSettings is not defined.




Notice: Undefined index: userSettings in /api/update-settings.php on
line 9




However, if I set the settings variable as a different data type (such as an int or string), the index is no longer undefined.



Within the PHP, when I dump request to see what's in it, userSettings is not found:



var_dump($_REQUEST);
Output: array(1) { ["userId"]=> string(1) "1" }


I'm adding items to my array using settings['template'] = template;



Before I submit the ajax request, I can console log it and I get this:



[p: "setting1", s: 1587, emp: "setting2", ems: 3245, template: "", …]


which contains all data that I need.



I tried to stringify the data with JSON.stringify(settings) but then ajax passes an empty array:



array(2) { ["userId"]=> string(1) "1" ["userSettings"]=> string(2) "" } 


I feel like this is something simple, but I can't figure it out. And yes, I've done my searches - I've found similar things, but none of the responses seem to help.



Thank you, in advance.










share|improve this question















I am trying to pass an array in an array via ajax. If I do not pass in the additional array, it works just fine.



For example:



var settings = ;
// add stuff to the array

$.ajax({
type: 'POST',
url: "api/update-settings",
data: {
userId: 1,
userSettings: settings
},
done: function(response) {
//do something with the response
},
fail: function() {
// do error stuff
}
});


Sending this will not work. The API (in PHP) gets the code and is able to tell me what userId is, but userSettings is not defined.




Notice: Undefined index: userSettings in /api/update-settings.php on
line 9




However, if I set the settings variable as a different data type (such as an int or string), the index is no longer undefined.



Within the PHP, when I dump request to see what's in it, userSettings is not found:



var_dump($_REQUEST);
Output: array(1) { ["userId"]=> string(1) "1" }


I'm adding items to my array using settings['template'] = template;



Before I submit the ajax request, I can console log it and I get this:



[p: "setting1", s: 1587, emp: "setting2", ems: 3245, template: "", …]


which contains all data that I need.



I tried to stringify the data with JSON.stringify(settings) but then ajax passes an empty array:



array(2) { ["userId"]=> string(1) "1" ["userSettings"]=> string(2) "" } 


I feel like this is something simple, but I can't figure it out. And yes, I've done my searches - I've found similar things, but none of the responses seem to help.



Thank you, in advance.







php jquery arrays ajax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 20:03

























asked Nov 8 at 19:23









Apolymoxic

163318




163318








  • 1




    Can you verify that you are actually sending all of the data via the network tab? Check this out stackoverflow.com/a/21617685/2191572 and find your "Request Headers". It's also possible that you are having cache issues or you are testing the completely wrong page.
    – MonkeyZeus
    Nov 8 at 19:32








  • 2




    Are you sure you're actually "adding stuff to the (settings) array"? Because if you're not, I'm pretty sure it won't send anything for an empty userSettings array in the request's body.
    – Jeto
    Nov 8 at 19:38












  • @MonkeyZeus - Well, if I change the variable to NOT be an array, it sends, so I am assuming it's sending.
    – Apolymoxic
    Nov 8 at 19:53










  • @Jeto Yes, I am sure. If I do a console.log I get the full array filled with the correct data.
    – Apolymoxic
    Nov 8 at 19:54






  • 1




    @Jeto is correct anyways, jQuery will not send an empty array like that. You can verify by doing var settings = [ 'yay something!' ]; instead
    – MonkeyZeus
    Nov 8 at 20:01
















  • 1




    Can you verify that you are actually sending all of the data via the network tab? Check this out stackoverflow.com/a/21617685/2191572 and find your "Request Headers". It's also possible that you are having cache issues or you are testing the completely wrong page.
    – MonkeyZeus
    Nov 8 at 19:32








  • 2




    Are you sure you're actually "adding stuff to the (settings) array"? Because if you're not, I'm pretty sure it won't send anything for an empty userSettings array in the request's body.
    – Jeto
    Nov 8 at 19:38












  • @MonkeyZeus - Well, if I change the variable to NOT be an array, it sends, so I am assuming it's sending.
    – Apolymoxic
    Nov 8 at 19:53










  • @Jeto Yes, I am sure. If I do a console.log I get the full array filled with the correct data.
    – Apolymoxic
    Nov 8 at 19:54






  • 1




    @Jeto is correct anyways, jQuery will not send an empty array like that. You can verify by doing var settings = [ 'yay something!' ]; instead
    – MonkeyZeus
    Nov 8 at 20:01










1




1




Can you verify that you are actually sending all of the data via the network tab? Check this out stackoverflow.com/a/21617685/2191572 and find your "Request Headers". It's also possible that you are having cache issues or you are testing the completely wrong page.
– MonkeyZeus
Nov 8 at 19:32






Can you verify that you are actually sending all of the data via the network tab? Check this out stackoverflow.com/a/21617685/2191572 and find your "Request Headers". It's also possible that you are having cache issues or you are testing the completely wrong page.
– MonkeyZeus
Nov 8 at 19:32






2




2




Are you sure you're actually "adding stuff to the (settings) array"? Because if you're not, I'm pretty sure it won't send anything for an empty userSettings array in the request's body.
– Jeto
Nov 8 at 19:38






Are you sure you're actually "adding stuff to the (settings) array"? Because if you're not, I'm pretty sure it won't send anything for an empty userSettings array in the request's body.
– Jeto
Nov 8 at 19:38














@MonkeyZeus - Well, if I change the variable to NOT be an array, it sends, so I am assuming it's sending.
– Apolymoxic
Nov 8 at 19:53




@MonkeyZeus - Well, if I change the variable to NOT be an array, it sends, so I am assuming it's sending.
– Apolymoxic
Nov 8 at 19:53












@Jeto Yes, I am sure. If I do a console.log I get the full array filled with the correct data.
– Apolymoxic
Nov 8 at 19:54




@Jeto Yes, I am sure. If I do a console.log I get the full array filled with the correct data.
– Apolymoxic
Nov 8 at 19:54




1




1




@Jeto is correct anyways, jQuery will not send an empty array like that. You can verify by doing var settings = [ 'yay something!' ]; instead
– MonkeyZeus
Nov 8 at 20:01






@Jeto is correct anyways, jQuery will not send an empty array like that. You can verify by doing var settings = [ 'yay something!' ]; instead
– MonkeyZeus
Nov 8 at 20:01














2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Considering the way you're adding values to your settings variable, what you want to manipulate/send is an object, not an array.



You need to change your declaration from:



var settings = ;


to:



var settings = {};





share|improve this answer























  • That's it. I knew it was something simple. Thank you!
    – Apolymoxic
    Nov 8 at 20:11










  • You're welcome :)
    – Jeto
    Nov 8 at 20:11


















up vote
0
down vote













Have you tried serializing the array? You can find this approach here, here and here.



so it could end up like



var settings = ;
// add stuff to the array

$.ajax({
type: 'POST',
url: "api/update-settings",
data: {
userId: 1,
userSettings: JSON.stringify(settings);//here is the change
},
done: function(response) {
//do something with the response
},
fail: function() {
// do error stuff
}
});


hope this helps.






share|improve this answer





















  • I tried that, too. It does send more, but only a blank array. I'll add this in my question
    – Apolymoxic
    Nov 8 at 19:56











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214760%2ferror-while-sending-array-in-array-using-ajax%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Considering the way you're adding values to your settings variable, what you want to manipulate/send is an object, not an array.



You need to change your declaration from:



var settings = ;


to:



var settings = {};





share|improve this answer























  • That's it. I knew it was something simple. Thank you!
    – Apolymoxic
    Nov 8 at 20:11










  • You're welcome :)
    – Jeto
    Nov 8 at 20:11















up vote
2
down vote



accepted










Considering the way you're adding values to your settings variable, what you want to manipulate/send is an object, not an array.



You need to change your declaration from:



var settings = ;


to:



var settings = {};





share|improve this answer























  • That's it. I knew it was something simple. Thank you!
    – Apolymoxic
    Nov 8 at 20:11










  • You're welcome :)
    – Jeto
    Nov 8 at 20:11













up vote
2
down vote



accepted







up vote
2
down vote



accepted






Considering the way you're adding values to your settings variable, what you want to manipulate/send is an object, not an array.



You need to change your declaration from:



var settings = ;


to:



var settings = {};





share|improve this answer














Considering the way you're adding values to your settings variable, what you want to manipulate/send is an object, not an array.



You need to change your declaration from:



var settings = ;


to:



var settings = {};






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 20:11

























answered Nov 8 at 20:10









Jeto

3,85511017




3,85511017












  • That's it. I knew it was something simple. Thank you!
    – Apolymoxic
    Nov 8 at 20:11










  • You're welcome :)
    – Jeto
    Nov 8 at 20:11


















  • That's it. I knew it was something simple. Thank you!
    – Apolymoxic
    Nov 8 at 20:11










  • You're welcome :)
    – Jeto
    Nov 8 at 20:11
















That's it. I knew it was something simple. Thank you!
– Apolymoxic
Nov 8 at 20:11




That's it. I knew it was something simple. Thank you!
– Apolymoxic
Nov 8 at 20:11












You're welcome :)
– Jeto
Nov 8 at 20:11




You're welcome :)
– Jeto
Nov 8 at 20:11












up vote
0
down vote













Have you tried serializing the array? You can find this approach here, here and here.



so it could end up like



var settings = ;
// add stuff to the array

$.ajax({
type: 'POST',
url: "api/update-settings",
data: {
userId: 1,
userSettings: JSON.stringify(settings);//here is the change
},
done: function(response) {
//do something with the response
},
fail: function() {
// do error stuff
}
});


hope this helps.






share|improve this answer





















  • I tried that, too. It does send more, but only a blank array. I'll add this in my question
    – Apolymoxic
    Nov 8 at 19:56















up vote
0
down vote













Have you tried serializing the array? You can find this approach here, here and here.



so it could end up like



var settings = ;
// add stuff to the array

$.ajax({
type: 'POST',
url: "api/update-settings",
data: {
userId: 1,
userSettings: JSON.stringify(settings);//here is the change
},
done: function(response) {
//do something with the response
},
fail: function() {
// do error stuff
}
});


hope this helps.






share|improve this answer





















  • I tried that, too. It does send more, but only a blank array. I'll add this in my question
    – Apolymoxic
    Nov 8 at 19:56













up vote
0
down vote










up vote
0
down vote









Have you tried serializing the array? You can find this approach here, here and here.



so it could end up like



var settings = ;
// add stuff to the array

$.ajax({
type: 'POST',
url: "api/update-settings",
data: {
userId: 1,
userSettings: JSON.stringify(settings);//here is the change
},
done: function(response) {
//do something with the response
},
fail: function() {
// do error stuff
}
});


hope this helps.






share|improve this answer












Have you tried serializing the array? You can find this approach here, here and here.



so it could end up like



var settings = ;
// add stuff to the array

$.ajax({
type: 'POST',
url: "api/update-settings",
data: {
userId: 1,
userSettings: JSON.stringify(settings);//here is the change
},
done: function(response) {
//do something with the response
},
fail: function() {
// do error stuff
}
});


hope this helps.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 19:52









Angel Ulise

32




32












  • I tried that, too. It does send more, but only a blank array. I'll add this in my question
    – Apolymoxic
    Nov 8 at 19:56


















  • I tried that, too. It does send more, but only a blank array. I'll add this in my question
    – Apolymoxic
    Nov 8 at 19:56
















I tried that, too. It does send more, but only a blank array. I'll add this in my question
– Apolymoxic
Nov 8 at 19:56




I tried that, too. It does send more, but only a blank array. I'll add this in my question
– Apolymoxic
Nov 8 at 19:56


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214760%2ferror-while-sending-array-in-array-using-ajax%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff