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?

4
  • 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? Commented Feb 4, 2013 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! Commented Feb 4, 2013 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. Commented Feb 4, 2013 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! Commented Feb 4, 2013 at 15:09

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the suggestion, this is what I went for. Replaced the &s on my string for |s and the server can split and use it.
Hm. If I'm not missing something important, you should be able to use & same way as |. Without any replacement.
Probably. But why to risk? I'm sure I can use |. Better safe than sorry!
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.

1 Comment

That didn't solve the problem but I didn't know about the contentType param. Thank you.
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

3 Comments

+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.
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.
thanks, by the way, correcting what you said (gameower) the server will get an array not a string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.