3

Similar to this question I need to send a string as data in a post request.

Unlike that one, I can't use an object because I have repeated items. As you can see in my sample data sn1, sn2 and sn3 are repeated several times on different datetimes.

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 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 triggers failed.

Can I send it as a string? If not, how can I send multiple items with the same key?

Community
  • 1
  • 1
caiocpricci2
  • 7,714
  • 10
  • 56
  • 88
  • 3
    Does the Network tab in the debugger console show an error? what is the error in the return argument of the error function? Also, change your error function to `error: function(jqXHR, textStatus, errorThrown)`, then you can see the error message in `errorThrown`. you can also get more information inspecting the other 2 arguments in the error event handler. What is the error message you get? – Nope Feb 04 '13 at 13:28
  • I don't have a network tab. I'm developing this using phonegap so I don't have access to the amazing debugger console. I didn't bother to log everything as we went with another solution! Thank you! – caiocpricci2 Feb 04 '13 at 14:43
  • Ah, I see. At least you can make use of the error handler arguments in future if you ever need to. sorry about that, didn't realise you were using phonegap. – Nope Feb 04 '13 at 14:51
  • 1
    Probably because I didn't mentioned it :). I get half of the usual replies if I add phonegap in the tag. Your comment was very helpful mostly because I've never noticed we had a Network tab in the console, and it's very handy. I come from a mobile background and phonegap hates me. So every bit helps! Thanks! – caiocpricci2 Feb 04 '13 at 15:09

3 Answers3

2
    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 server you will be able to get it like: screensString = Request["screens"]. After that, screensString will contain a single string with all screens.

Viktor S.
  • 12,736
  • 1
  • 27
  • 52
1

When you don't specify contentType in the ajax options, your request will default to 'application/x-www-form-urlencoded; charset=UTF-8'. However, when your post data is just text, you should make the server aware of that fact by specifying a contentType 'text'. As opposed to the contentType, the dataType specifies the type of response data that you expect back from the server.

marty
  • 4,005
  • 22
  • 19
1

I think what you need is to use [] in your parameters.

instead of sending &sn3= multiple times (which is rewriting itself) send it as an array like this &sn3[]=

if you are getting this data from an form input use name="sn3[]" and if this is the case, I would recommend you use $('#yourform').serialize() as data sent

w3jimmy
  • 692
  • 10
  • 21
  • +1, that is a good suggestion as well, but we went for the other one. The server would have to parse the string anyway and this was extra work for the client. – caiocpricci2 Feb 04 '13 at 14:44
  • Just as note: it depends on server how `[]` will be handled. In php you will get nice array. In asp.net - you will get one item which contains all `sn3[]` values separated with `|`, as I remember. – Viktor S. Feb 04 '13 at 14:47
  • thanks, by the way, correcting what you said (gameower) the server will get an array not a string – w3jimmy Feb 04 '13 at 16:13