Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
126
rated 0 times [  127] [ 1]  / answers: 1 / hits: 15775  / 10 Years ago, fri, may 16, 2014, 12:00:00
$.ajax( { url : '', data: {}, dataType:'jsonp',  jsonpCallback: 'callbackName', type: 'post'
,success:function (data) {
console.log('ok');
},
error:function () {
console.log('error');
}
});


How do I write the same functionality in pure JS?


More From » ajax

 Answers
19

In this particular case, you aren't making an ajax call at all, instead you're making a JSONP request. Luckily, these are incredibly easy to replicate and work in all browsers.



var s = document.createElement(script),
callback = jsonpCallback_ + new Date().getTime(),
url = http://forexplay.net/ajax/quotes.php?callback= + callback;
window[callback] = function (data) {
// it worked!
console.log(data);
};
s.src = url;
document.body.appendChild(s);

[#70973] Thursday, May 15, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marib

Total Points: 596
Total Questions: 120
Total Answers: 95

Location: Nauru
Member since Thu, Feb 2, 2023
1 Year ago
;