Friday, February 17, 2012

An easy way to post to a URL and not use ajax?


Using jQuery, I know $.ajax() can be used to POST to a url but I can't use $.ajax() for my problem. I want the client to POST to a url and have the server redirect to a user to some url (PRG pattern) so therefore, it cannot use XHR requests.



How can I get the client to POST to a url without creating a <form> ? Surely, there's got to be an easier solution than this. jQuery post request (not Ajax)

3 comments:

  1. Why can't you POST with Ajax, and then whenever it returns, do a Javascript redirect within the callback function? Just have the server provide the URL to redirect to as a response.

    ReplyDelete
  2. You can create and send a form or use ajax. There is no other way I know of.

    But why not: First save the data using ajax post and then go to the new page.

    $.post('youscript.php', function(data) {
    window.location.href = data;
    });


    Otherwise see this old question on how to send it with a dynamically created form.

    ReplyDelete
  3. simplest approach is to use jquery and click() events. and passing them as var's in a dataset using data: {data1: datavals}

    ill edit this post once the code is written.

    update:

    <input type="text" name="data1" id="data1" value="" placeholder="Input text for data 1">
    <input type="text" name="data2" id="data2" value="" placeholder="Input text for data 2">
    <input type="submit" name="submit" id="submit" value="submit">
    $("#submit").click(function(){
    $.ajax({
    url: "process.php",
    data: {data1: $("#data1").val(), data2: $("#data2").val()},
    dataType: "json",
    type: "POST"

    });
    });

    ReplyDelete