Its a bit unclear how Javascript internally is doing the request. I know its not using the build-in
browser XMLHttpRequest, but how does it do it? Read an article on stackoverflow where they simply create a Javascript
object and set the
var obj = document.createElement(‘script’);
obj.src = “http://somedomain.com?blabla=yes“
Using JavaScript to perform a GET request without AJAX
Here from jQuery:
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'jsonCallback',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
console.log("console. hurra");
},
error: function(e) {
console.log(e.message);
}
});
Do you know how does jQuery internally build up the request in Javascript and submit it?
I tried this locally and it worked like charm in the sense that the the script was inserted correctly in my page.
var CampaignNs = {
GET: function(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 advance thanks
Good article here:
http://johnnywey.wordpress.com/2012/05/20/jsonp-how-does-it-work/#comment-359
Using XHTTPRequest or inserting tags are both valid ways to trigger GET requests. XHTTPRequests are cleaner in many ways tough because they don’t involve messing around in the DOM.
This makes no sense. JavaScript (i.e. the JavaScript implementation in your browser) need not rely on XHTTPRequest, it just invokes the browser’s HTTP APIs if it needs to do requests. Which it doesn’t need to do except through XHTTPRequest 🙂