最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Using JavaScript to perform a GET request without AJAX - Stack Overflow

programmeradmin2浏览0评论

Out of curiosity, I'm wondering about the best (easiest, fastest, shortest, etc; make your pick) way to perform a GET request in JavaScript without using AJAX or any external libraries.

It must work cross-browser and it's not allowed to distort the hosting web page visually or affect it's functionality in any way.

I don't care about headers in the request, just the url-part. I also don't care about the result of the request. I just want the server to do something as a side effect when it receives this request, so firing it is all that matters. If your solution requires the servers to return something in particular, that's ok as well.

I'll post my own suggestion as a possible answer, but I would love it if someone could find a better way!

Out of curiosity, I'm wondering about the best (easiest, fastest, shortest, etc; make your pick) way to perform a GET request in JavaScript without using AJAX or any external libraries.

It must work cross-browser and it's not allowed to distort the hosting web page visually or affect it's functionality in any way.

I don't care about headers in the request, just the url-part. I also don't care about the result of the request. I just want the server to do something as a side effect when it receives this request, so firing it is all that matters. If your solution requires the servers to return something in particular, that's ok as well.

I'll post my own suggestion as a possible answer, but I would love it if someone could find a better way!

Share Improve this question asked Dec 20, 2010 at 13:38 JakobJakob 24.4k8 gold badges47 silver badges58 bronze badges 5
  • Do you need to work with the return value, or is making the request enough? – Pekka Commented Dec 20, 2010 at 13:40
  • 1 This is a really wierd thing to do. Why wouldn't you just make a request via AJAX? – annakata Commented Dec 20, 2010 at 13:43
  • 1 One could create an iframe dynamically, and load the resource in there. If it's on the same domain, you can even access the return data – Pekka Commented Dec 20, 2010 at 13:45
  • @annakata: this piece of code is required in an environment where I can't use external libraries and cross-browser AJAX in that case is a little messy. An easier solution should be possible (especially since I don't need something returned from the request) – Jakob Commented Dec 20, 2010 at 13:47
  • @Jakob - Messier than a clunky create/remove script element which achieves what you want almost as a side effect? A fire-and-forget AJAX GET is the correct solution because it does exactly what you want and no more. – annakata Commented Dec 20, 2010 at 14:15
Add a ment  | 

3 Answers 3

Reset to default 9

Have you tried using an Image object? Something like:

var req = new Image();
req.onload = function() {
    // Probably not required if you're only interested in
    // making the request and don't need a callback function
}
req.src = 'http://example./foo/bar';
function GET(url) {
  var head = document.getElementsByTagName('head')[0];
  var n = document.createElement('script');
  n.src = url;
  n.type = 'text/javascript';
  n.onload = function() { // this is not really mandatory, but removes the tag when finished.
    head.removeChild(n);
  };
  head.appendChild(n);
}

I would go with Pekka idea and use hidden iframe, the advantage is that no further parsing will be done: for image, the browser will try to parse the result as image, for dynamically creating script tag the browser will try to parse the results as JavaScript code.. iframe is "hit and run", the browser doesn't care what's in there.

Changing your own solution a bit:

function GET(url) {
    var oFrame = document.getElementById("MyAjaxFrame");
    if (!oFrame) {
        oFrame = document.createElement("iframe");
        oFrame.style.display = "none";
        oFrame.id = "MyAjaxFrame";
        document.body.appendChild(oFrame);
    }
    oFrame.src = url;
}
发布评论

评论列表(0)

  1. 暂无评论