0

I'm trying to request a page and click a button on it without opening a window so I'm thinking post could work. Here is my code:

<!DOCTYPE html>
<html>
    <head>
        <title>test</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"</script>
        <script>
            $(document).ready(function() {  
                $.post(
                    "http://m.roblox.com/Catalog/VerifyPurchase?assetid=161075864&type=tickets&expectedPrice=1",
                    $("ui-block-a").submit();
                );
                alert("work");
            });
        </script>
</head>
<body>
</body>
</html>

If I'm understanding post correctly, the first argument is the url to which it requests from and the second argument is the one where you can send data. I'm trying to send a request to click the "buy" button on the page if you follow the link. Can anyone help?

JAAulde
  • 19,250
  • 5
  • 52
  • 63
nPwn
  • 13
  • 5
  • That isn't quite what a post does. It does send data to the server, but the server chooses what to do with it. To do what you want, the server would have to support it. – dpk2442 Jun 14 '14 at 03:05
  • Is is possible to send data that would click a button, assuming it does accept the data? – nPwn Jun 14 '14 at 03:06
  • Well because it's a submit button, you could always do a request to the form that the button submits, which would kind of simulate what you want. Doing exactly what you want isn't really possible. If it was, it would create some pretty big security problems. – dpk2442 Jun 14 '14 at 03:12
  • That makes sense. I guess my problem now is that is isn't classified as a button, just a div with input. – nPwn Jun 14 '14 at 03:16
  • Imagine if someone could simulate you interacting on a site like PayPal. They could simulate you sending them money without you ever knowing. In any case, what are you trying to accomplish here? – dpk2442 Jun 14 '14 at 03:18
  • Yeah I understand, I'm trying to click a button on a webpage without actually having to open it in a window. But if that isn't possible with a window will do. – nPwn Jun 14 '14 at 03:29

3 Answers3

1

A post request sends data to a given URL. What the server does with that data from that point is entirely up to the server. It is very unlikely that the web page you are trying to simulate a button press on allows that behavior. If that was possible in general, it would leave users open to some very large security vulnerabilities. For example, a site could simulate donating money via PayPal without the user ever knowing.

In this particular case, the button appears to submit a form. In that case, you could always attempt to send the request directly to the page that the form submits to, which would simulate submitting the form. However form submissions are generally protected against stuff like this, because again, it could be used to act on behalf of the user.

Basically, the best option for something like this is to provide the user with instructions detailing what they need to do, and then open the page for them.

dpk2442
  • 701
  • 3
  • 8
0

Assuming there is no other issues like CORS you can do the post like you are trying to do, but the following is invalid.

$.post( "http://m.roblox.com/Catalog/VerifyPurchase?assetid=161075864&type=tickets&expectedPrice=1", $("ui-block-a").submit();
  1. You are doing a POST which required the data being send to the server to be in the request body and not the URL string.

  2. When you post to a page you are actually posting directly to the server and not a page and in this case unless the $("ui-block-a").submit(); is returning data for the page you are posting to, it is not needed.

The following would be closer to what you are looking for with that post

$.post( "http://m.roblox.com/Catalog/VerifyPurchase", { assetid: "161075864", type: "2pticketsm", expectedPrice: "1" } );

If it was successful you should see a allow-orgins error in your developer / inspector console.

MattSizzle
  • 3,145
  • 1
  • 22
  • 42
0

$.post() is an abbreviated form of $.ajax(), with POST pre-selected as type. There are also $.get() (with GET pre-selected as type), and $.load() (with the returned data immediately injected into the specified element). But $.ajax() is the grand-daddy of them all.

AJAX is a method of exchanging data with a processor file on a server, without leaving / refreshing the page you are on. That is, with AJAX (or $.post), you can send information to a processing page on the server -- such as: my_processor.php -- the processing page can do something with the data (for example, use the data to query a database), and then echo out data, which is returned to the originating page. The received data can then be injected into a DIV on the original page, or something of the sort.

An ajax routine is usually triggered by some event on the originating page (the user presses a button, or selects a value in a drop-down, or some such). Javascript (or jQuery) code detects the event, and the AJAX code is usually actioned in the javascript event.

Here are some simple examples of what has been described.

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111