Log in

View Full Version : Need to issue HTTP Get without opening window



RichardU
12-09-2013, 05:07 PM
I'm hoping this is simple. I need to be able to click a link and issue an HTTP Get (as if I were opening a new window) without actually opening a window. I don't need any feedback from the get.

Thanks,

Richard

jscheuer1
12-09-2013, 07:23 PM
Could you be more specific? Like any specific URL? Any specific reason you need to do this?

Anyways, you can issue a GET via AJAX. Nothing will open, you will get something back (response codes and if the request is successful, the data from the request), but you don't have to use any of that. The easiest way to send the request is with jQuery. If the page already uses jQuery, and you don't care about the specifics, just do:


<a href="#" onclick="jQuery.ajax({url: this.href}); return false;">Send a Get that Does Nothing</a>

djr33
12-09-2013, 07:50 PM
Are you trying to send information to the server but not see the response?

1. This is possible. But note that it's usually a good idea to still get the response if possible and display an error for the user if it fails. It would be a problem if important data isn't saved.

2. There's a way around this (if and only if: 1) you're using GET; 2) you do not need any response) that will avoid AJAX and avoid any possible cross-domain or other security issues. You can use an image. It sounds odd, but it works well in basically any browser that allows any JS at all. What you will do is the following:
i) Load something like "image.jpg?variable=value" into the page (you can load it into a hidden element if you want or even display the results to show the user it was successful).
ii) On the server, use PHP (or another serverside language) to treat that image as a PHP script, where instead of just showing an image file, it actually runs PHP and takes the information from the GET request and stores it (or whatever you want).
Further notes:
--Due to security restrictions, you can't do much of anything in JS to confirm that it actually worked (although you can, I believe, perhaps detect if the image loads or does not load). You can't, for example, check the filesize of the image to see if it's a binary yes/no response. (I've looked into this as a way around cross-browser AJAX restrictions, and it's blocked on the JS-receiving end.)
--In order to process an image as PHP, you have three options: i) use "image.php" as the URL and use PHP to send image-format headers along with image data; ii) do basically the same but also call it "image.jpg" and tell the server to treat ".jpg" as a PHP file (best for this file only) [the logic is exactly like working with .html files as PHP code, tutorials available online]; or iii) modification of ii: use .htaccess mod_rewrite to silently redirect the request and serve image.php AS the request for image.jpg.


If that all sounds like way more than you want to deal with, feel free to ignore this post (though it may help others with a similar question). If you just want AJAX, then John's reply will be fine.

I suppose one use for this could just be to say "I'm still here" rather than letting the system do an automatic logoff for inactivity.

RichardU
12-09-2013, 10:04 PM
Thanks for the help guys. I have a home automation server that can respond to HTTP triggers. I don't have the ability to reprogram the server, but the ajax solution sounds like it would work.

To be specific, the URL I want to send looks like this: http://myhost:9999/Test?1=50

So would this be correct?

<a href="#" onclick="jQuery.ajax({http://myhost:9999/Test?1=50: this.href}); return false;">Send a Get that Does Nothing</a>

Thanks again.

keyboard
12-09-2013, 10:27 PM
I believe it should be


<a href="#" onclick="jQuery.ajax({url:'http://myhost:9999/Test?1=50'}); return false;">Send a Get that Does Nothing</a>

jscheuer1
12-09-2013, 10:59 PM
I believe it should be


<a href="#" onclick="jQuery.ajax({url:'http://myhost:9999/Test?1=50'}); return false;">Send a Get that Does Nothing</a>


That's right, I think. To be on the safe side specify the url and the data separately:


<a href="#" onclick="jQuery.ajax({url: 'http://myhost:9999/Test', data: '1=50'}); return false;">Send a 1 = 50 Get to myhost:9999/Test</a>

And, as I was saying, you have to have jQuery already loaded on the page.

Something else occurred to me though. Since you don't care if you get anything back, you could target a hidden frame or iframe with a normal link.

RichardU
12-10-2013, 01:33 PM
You guys are great. I'll try the hidden frame trick later today.

Thanks,

Richard