Reputation: 129
Usually we use XMLHttpRequest object for ajax requests using Javascript. Is there a way to use ajax with Javascript without using XMLHttpRequest object. For example, on page load I need to hit a URL and get the response and manipulate it, using javascript without XMLHttpRequest object.
Upvotes: 0
Views: 4294
Reputation: 3440
You can try creating a hidden iframe, but you must be aware of: Same Origin Policy.
Here is what a hidden iframe looks like:
<script language="JavaScript" type="text/javascript">
function alertBobo(alertText) {
alert(alertText);
}
ifrm = document.createElement("IFRAME");
ifrm.setAttribute("src", "/iframe.html");
ifrm.setAttribute("id","myIframe");
ifrm.setAttribute("style","display:none");
ifrm.style.width = 0+"px";
ifrm.style.height = 0+"px";
document.body.appendChild(ifrm);
</script>
Then from iframe.html (the URL you would have called with Ajax if you were doing XMLHttpRequest), but you are not, we are creating hidden iframe.
You can call functions from parent and transfer data like this:
<script language="JavaScript" type="text/javascript">
parent.alertBobo("Iframe Calling");
</script>
And you will get alert with "Iframe Calling" text...
You can also transfer JSON for example to parent window.
Upvotes: 0
Reputation: 3748
Fetch API
is one possible alternative to XMLHttpRequest
:
The Fetch API provides an interface for fetching resources (including across the network). It will seem familiar to anyone who has used
XMLHttpRequest
, but the new API provides a more powerful and flexible feature set.
Upvotes: 2