I need to do an HTTP GET request in JavaScript. What's the best way to do that?
I need to do this in a Mac OS X dashcode widget.
I need to do an HTTP GET request in JavaScript. What's the best way to do that?
I need to do this in a Mac OS X dashcode widget.
The best way is to use AJAX ( you can find a simple tutorial on this page Tizag). The reason is that any other technique you may use requires more code, it is not guaranteed to work cross browser without rework and requires you use more client memory by opening hidden pages inside frames passing urls parsing their data and closing them. AJAX is the way to go in this situation. That my two years of javascript heavy development speaking.
To do this Fetch API is the recommended approach, using JavaScript Promises. XMLHttpRequest (XHR), IFrame object or dynamic tags are older (and clunkier) approaches.
Here is a great fetch demo and MDN docs
Lots of great advice above, but not very reusable, and too often filled with DOM nonsense and other fluff that hides the easy code.
Here's a Javascript class we created that's reusable and easy to use. Currently it only has a GET method, but that works for us. Adding a POST shouldn't tax anyone's skills.
Using it is as easy as:
To refresh best answer from joann with promise this is my code:
You can get an HTTP GET request in two ways:
This approach based on xml format. You have to pass the URL for the request.
This one is based on jQuery. You have to specify the URL and function_name you want to call.
In your widget's Info.plist file, don't forget to set your
AllowNetworkAccess
key to true.