Skip to main content

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)


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

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?