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.
php jquery arrays ajax
|
show 3 more comments
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.
php jquery arrays ajax
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 emptyuserSettings
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 doingvar settings = [ 'yay something!' ];
instead
– MonkeyZeus
Nov 8 at 20:01
|
show 3 more comments
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.
php jquery arrays ajax
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
php jquery arrays ajax
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 emptyuserSettings
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 doingvar settings = [ 'yay something!' ];
instead
– MonkeyZeus
Nov 8 at 20:01
|
show 3 more comments
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 emptyuserSettings
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 doingvar 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
|
show 3 more comments
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 = {};
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
add a comment |
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.
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
add a comment |
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 = {};
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
add a comment |
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 = {};
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
add a comment |
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 = {};
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 = {};
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 emptyuserSettings
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