1

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)

Community
  • 1
  • 1
burnt1ce
  • 14,387
  • 33
  • 102
  • 162
  • without creating "?" ?? You can make a redirection using js, so I don't see why you cannot use ajax... – soju Feb 13 '12 at 16:19
  • 1
    Maybe someone smarter than me will contridict me, but in my experience, you need either a form or ajax. Sorry. – Matt Grande Feb 13 '12 at 16:21
  • 1
    possible duplicate of [jQuery non-AJAX POST](http://stackoverflow.com/questions/5524045/jquery-non-ajax-post) – Brad Feb 13 '12 at 16:22
  • you need ajax in order to make the call.. you dont necessarily need a form though.. see my answer for an ajax way of doing this. – NDBoost Feb 13 '12 at 16:24
  • @Brad - That solution is simple but it still creates a form. – burnt1ce Feb 13 '12 at 16:32
  • @burnt1ce: You are losing the session data during a redirect? I think that is your problem! – PiTheNumber Feb 13 '12 at 16:36
  • @burnt1ce, Why don't you want to create a form? It doesn't appear on your page... it's in the background. – Brad Feb 13 '12 at 16:37
  • The challenge is not how do I POST to a url. I know I can create an invisible
    to POST to a url but I had a gut feeling that there would be a better solution. The problem occurs frequently and the solution feels like it came from the web 1.0 days. I was hoping to find a better/cleaner solution. If I can create an AJAX request to POST to a url without creating a
    , why can't I do the same without using an AJAX request?
    – burnt1ce Feb 13 '12 at 16:48
  • @burnt1ce Because you need to send your data somehow. I understand your problem but I am afraid the is no better solution then an ajax post. – PiTheNumber Feb 13 '12 at 16:53
  • @PiTheNumber I can still send my data using a POST request without ajax. But I agree that there's possibly no better solution than to create a
    – burnt1ce Feb 13 '12 at 16:59

3 Answers3

2

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.

Jordan
  • 31,971
  • 6
  • 56
  • 67
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.

Community
  • 1
  • 1
PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
0

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"

    });
});
NDBoost
  • 10,184
  • 6
  • 53
  • 73