0

I have an application/x-www-form-urlencoded string and a destination URL. How do I make the browser open that URL and use the string as the POST body?

EDIT: I know how to do that with a URL and an array (e.g. this), so a fool proof way to parse the string to an array would be an answer as well

Community
  • 1
  • 1
user69715
  • 775
  • 6
  • 15

2 Answers2

1

Warning: Completely untested code follows.

function post_form(url, body) {
    var form = document.createElement('form');
    form.method = 'POST';
    form.action = url;

    var kvs = body.split('&');
    for (var i = 0; i < kvs.length; i++) {
        var kv = kvs[i];
        var k, v, p = kv.indexOf('=');
        if (p >= 0) {
            k = kv.substring(0, p);
            v = kv.substring(p + 1);
        } else {
            k = kv;
            v = '';
        }
        k = decodeURIComponent(k);
        v = decodeURIComponent(v);

        var input = document.createElement('input');
        input.type = 'hidden';
        input.name = k;
        input.value = v;
        form.appendChild(input);
    }

    document.body.appendChild(form);
    form.submit();
}
melpomene
  • 79,257
  • 6
  • 70
  • 127
0

you can use jquery to post ajax link to ajax properties and default values. here is the post example

JQuery ajax request has default value

contentType : 'application/x-www-form-urlencoded; charset=UTF-8'

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: successcallback,
  dataType: dataType
});
Raj
  • 103
  • 5