AJAX request data string instead of object
Like this question I need to send a string as data in a post request.
In contrast, I cannot use an object because I have duplicate elements. As you can see in my sample data sn1, sn2 and sn3 are repeated multiple times on different dates.
Sample data:
&sn3=2013-2-4T12:43:52&sn3=2013-2-4T12:43:55&sn1=2013-2-4T12:43:59&sn1=2013-2-4T12:44:0&sn2=2013-2-4T12:44:0&sn3=2013-2-4T12:44:2&sn2=2013-2-4T12:44:3&sn3=2013-2-4T12:44:19&sn3=2013-2-4T12:44:19&sn3=2013-2-4T12:44:19&sn2=2013-2-4T12:44:19&sn3=2013-2-4T12:44:21&sn2=2013-2-4T12:44:22&sn2=2013-2-4T12:46:39&sn3=2013-2-4T12:46:42&sn2=2013-2-4T12:46:44&sn2=2013-2-4T12:46:45&sn2=2013-2-4T12:46:46&sn2=2013-2-4T12:47:27&sn2=2013-2-4T12:47:27&sn2=2013-2-4T12:49:44&sn2=2013-2-4T12:50:21&sn2=2013-2-4T12:52:21&sn2=2013-2-4T12:52:24&sn2=2013-2-4T12:57:35&sn3=2013-2-4T12:57:38&sn3=2013-2-4T12:57:39&sn2=2013-2-4T12:57:39&sn2=2013-2-4T12:57:40&sn3=2013-2-4T12:57:46&sn3=2013-2-4T13:21:30
I have tried using the following
console.log(screens); //logs my sample data posted above.
$.ajax({
url : url,
type: "POST",
dataType : 'text',
data : screens,
success : function(data) {
console.log("sucessfull sending:")
console.log(data);
},
error : function() {
console.log('failed');
}
});
But it always works.
Can I send it as a string? If not, how can I send multiple items with the same key?
source to share
console.log(screens); //logs my sample data posted above.
$.ajax({
url : url,
type: "POST",
dataType : 'text',
data : {screens:screens},
success : function(data) {
console.log("sucessfull sending:")
console.log(data);
},
error : function() {
console.log('failed');
}
});
. See data : {screens:screens},
, if you do something like that, on the server, you can get it as: screensString = Request["screens"]
. After that screenString will contain one string with all screens.
source to share
If you don't specify contentType in the ajax parameters, your request will default to "application / x-www-form-urlencoded; encoding = UTF-8". However, when your post data is just text, you have to tell this server about it by specifying the text contentType “text.” Unlike contentType, dataType specifies the data type of the response that you expect from the server.
source to share
I think you need to use [] in your parameters.
instead of sending &sn3=
multiple times (which overwrites itself) sends it as an array like this&sn3[]=
if you are getting this data from form input use name="sn3[]"
and if so i would recommend using $('#yourform').serialize()
as submitted data
source to share