How to get the value of the proposed JSONP using pure JavaScript?

Hello. Relatively long dealt with such things - retrieving data from jsonp. And at the moment itch to take javascript (without jquery and others).

That's our hero - JSONP himself:
response({
 "files": [
{
 "type": "js",
 "name": "script"
},
{
 "type": "html",
 "name": "file"
}
]
});


But, in fact, so I tried to type:
// ...
function ajax(url, callback, parse) {
 var xhr = new XMLHttpRequest();
 xhr.open('GET', url);
 xhr.onload = function(){
 if (this.readyState == 4) {
 if (xhr.status >= 200 && xhr.status < 400) {
 callback(the parse ? JSON.parse(xhr.responseText) : xhr.responseText);
}
}
};
xhr.send(null);
}

ajax(jsonp_url, function(data) {
 data.files.forEach(function(item, i){
alert(data.item.type);
});
});
// ...


This was another attempt, but stopped on the forEach... but the forEach still doesn't want to work. The main problem lies in extracting the type, but not in a forEach.

PS Jquery request not to offer.
July 8th 19 at 12:05
2 answers
July 8th 19 at 12:07
Solution
The problem was solved.
Got rid of the callback got a JSON (without "P").
Then used JSON.parse() on the JSON (in my function ajax last parameter indicated true), got a value described in the question forEach. Good luck:)
July 8th 19 at 12:09
This is not JSON, but JSONP. JSON looks like this:
{
 "files": [
{
 "type": "js",
 "name": "script"
},
{
 "type": "html",
 "name": "file"
}
]
}
Thank you, corrected. - Rick_Pfannersti commented on July 8th 19 at 12:12
: So with JSONP and work differently, Google, or first result in JSON and then parse. - mazie.Towne commented on July 8th 19 at 12:15
thanks for the tip... Problem solved. In pure JS I'm not particularly strong, because of the prevalence everywhere of JQuery (ideally the same), which is now disposed of.

Will add a few thoughts: there are so many "alternatives" jquery with support for IE9 and above weight ~1-20KB (jq weighs ~80-120), also has ECMAScript 5 (for which I believe "now" is the time)... I Think every time to drag the library, which sometimes weighs more than website, simply unnecessary, especially when normally used only ~50% of its functionality. No doubt, the giants, squeezing out from the library, all of its potential, justify this weight. But simple sites with a minimum of images (and not only) who need often is just manipulation with the DOM (and other stuff) are just wasting traffic.
So, a slice of perfectionism (or stupidity - as), not more. - Rick_Pfannersti commented on July 8th 19 at 12:18
: 84 KB is nothing, try to count even on the worst Internet speeds and verify, and generally to whom as, for me personally, the main thing that was written faster, then either jQuery or even frameworks like Angular, React, etc. frameworks. - mazie.Towne commented on July 8th 19 at 12:21
sometimes when you just need to manipulate the DOM (as I am now, but still the ajax function written in length about 300 characters) - then just take the library, you can even similar in syntax with JQuery, but containing only DOM)) Yes, 84кб nothing, I agree, but I'm in such moments, always remember about cell phones, their traffic and speed (give an example even yourself with a long posting even in the same application VC)... You are right, especially with caching, this is nothing (although the first time depends on the speed of the Internet again))) However... Why not "show off" with the same balalaika.js? - Rick_Pfannersti commented on July 8th 19 at 12:24
: There are other projects that will drop people with a desktop PC, not high bar, I hate cell phones and mobile OS. - mazie.Towne commented on July 8th 19 at 12:27
well this whole mess I have with weight reduction sites through the use of other solutions, optimization, etc. perversions began with PageSpeed from Google and thinking about adaptive websites, which weigh sometimes 2 megabytes, and their cell phones, tablets, and USB modems accept with sadness. It's not only JQ and, in General, the approach to such things in General. My good, like, Internet websites weighing in at 2MB, sometimes significantly "impeded". Also do not like cell phones, but what to do... - Rick_Pfannersti commented on July 8th 19 at 12:30

Find more questions by tags JavaScriptJSONAJAXWeb Development