Learn XML HTTP Requests in JavaScript | AJAX Tutorial

Поделиться
HTML-код
  • Опубликовано: 27 янв 2018
  • XML Http Requests (AJAX) allows you to send and receive data from external files within your Javascript code without refreshing the page or re-running your script.
    In this video we look at how to create a request and then download the data, inserting it into the HTML body.
    For your reference:
    developer.mozilla.org/en-US/d...
    Follow me on Twitter @dcode
    If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!
    #dcode #javascript #webdev

Комментарии • 135

  • @dcode-software
    @dcode-software  6 лет назад +16

    UPDATE:
    Learn how to use AJAX to load data from an external file straight into your HTML tables:
    ruclips.net/video/12tjh_6xL2M/видео.html

  • @conanchambers466
    @conanchambers466 4 года назад

    Wow! I understand that syntax has changed but with your explaining my understanding has vastly improved so so much! Thankyou!

  • @mouthbreather280
    @mouthbreather280 5 лет назад +9

    Very Great Video! Codecademy doesn't even go into any detail or depth as for the purpose for the actual commands used. Really helped understanding the process more. Thanks for the great content and tutorial man! :)

  • @greatpandas99
    @greatpandas99 4 года назад +3

    You're a lifesaver man, I really appreciate this. Thanks

  • @erfelipe
    @erfelipe 3 года назад

    Quick, clean and direct. Thanks.

  • @99HAXXOR
    @99HAXXOR Год назад

    watching this video in 2022, you sir is "GREAT".
    it helped me a lot

  • @varletconstundefined
    @varletconstundefined 4 года назад +9

    amazing simple to the point.
    great introduction.
    i had zero knowledge before the video
    after it i googled for some other videos , and it was simple after your.
    Thanks man :)

    • @dcode-software
      @dcode-software  4 года назад +2

      No worries mate 😁

    • @varletconstundefined
      @varletconstundefined 4 года назад

      @@dcode-software one more question please
      do you know a good resource to study React
      i'm currently working on Fetching API
      and consuming it.
      then i wanna learn one framework.
      any good resource will help 😁

  • @wemode2998
    @wemode2998 3 года назад

    Great video. Concise and very informative

  • @GlenMillard
    @GlenMillard 4 года назад +1

    I learned quite a bit from your video - thanks much! It was excellent! :)

  • @owenharrison8012
    @owenharrison8012 4 года назад

    Great tutorial! Thank you very much!

  • @imcw
    @imcw 6 лет назад +5

    nice code editor + blue switch + good content + beautiful cover image. Love your videos, dcode!
    p.s: it would be great if you record in 60fps

    • @dcode-software
      @dcode-software  6 лет назад

      +Cương Trần Thank you! That means a lot - glad you enjoy 😁

    • @redsquirrel3893
      @redsquirrel3893 6 лет назад +1

      If you do do a high frame rate for Cương Trần and any others please keep the current 1080 option
      I actuality prefer it as the resolution matters way more for tutorials so you can see the code clearly and both make it take longer to load (my crappy Internet).
      1080 with the current frame rate is perfect for me.
      Also thanks keep up the good work.

    • @mouthbreather280
      @mouthbreather280 5 лет назад +1

      @
      Cương Trần There's no point. It's a text tutorial not a skateboarding tutorial that you'd rather see smoother motion. It will make such minuscule negligible difference for this kind of video. All it will accomplish is stressing dcode's computer more and making the file size larger. resolution matters over > fps in this case because we'd rather see the code sharp instead of blurry.

  • @Good-Enuff-Garage
    @Good-Enuff-Garage 2 года назад

    absolutely positively brilliant example

  • @Devronization
    @Devronization 5 лет назад +3

    Great video. Thanks a lot. I like your teaching style. Keep up the good work.

  • @vuedev6324
    @vuedev6324 2 года назад

    beautiful explanation of XMLHTTPREQUEST( ); TYSM ❤

  • @zenziiiiiiiiii
    @zenziiiiiiiiii 5 лет назад +2

    Thanx for making the video, I really needed this explanation.

  • @mistakenmillenial6834
    @mistakenmillenial6834 5 лет назад +4

    Thank you so much for this video. It helped me a lot.

  • @Emassei
    @Emassei 5 лет назад +4

    Thank you helped me out a lot!

  • @DanielHernandez-zx4kj
    @DanielHernandez-zx4kj 3 года назад

    Thank you so much for this video!!

  • @moddingdudes7055
    @moddingdudes7055 5 лет назад +2

    Thank you for this video very much. It fits very well when you read a little bit of the W3Schools page and I was still a little confused but you cleared all of that up and I thank you for that. I also believe in your xhr.readystatechange function you can use this.readystate and this.status instead of xhr.readystate or xhr.status. Thank you for your help though.

    • @dcode-software
      @dcode-software  5 лет назад

      No worries mate, using "xhr" over "this" depends on if you're using an arrow function or standard function

  • @samsam-zu7zc
    @samsam-zu7zc 4 года назад

    Your logo is beautifully designed

  • @jessicarosenberg4032
    @jessicarosenberg4032 4 года назад +1

    Thank you so much!!!! Just what I needed😊😊

  • @davidgrig4608
    @davidgrig4608 3 года назад

    great explanation thx

  • @yasirwisal8081
    @yasirwisal8081 4 года назад +1

    Well done Right on the point, thanks

  • @kleb2010
    @kleb2010 4 года назад +3

    Great video, helped a lot :)

  • @hyperspace2223
    @hyperspace2223 3 года назад

    Best XHR tutorial! Tried googling but couldn't find any helpful results.

  • @osasferguson
    @osasferguson 2 года назад

    Great content

  • @galbeeyutdmok651
    @galbeeyutdmok651 5 лет назад +28

    Hey Dom , do you like to use the DOM ?

  • @gokharol
    @gokharol 3 года назад

    Readystates:
    0 - unset
    1 - opened
    2 - headers_received
    3 - loading
    4 - done

  • @AlphyGacheru
    @AlphyGacheru 4 года назад

    Thank you sir!

  • @roffe8430
    @roffe8430 5 лет назад +2

    Awesome video! :D

  • @royalbhati7837
    @royalbhati7837 5 лет назад +2

    Amazing explanation :D

    • @dcode-software
      @dcode-software  5 лет назад

      Thank you mate, glad it helped

    • @royalbhati7837
      @royalbhati7837 5 лет назад +1

      @@dcode-software Just saw few of your videos and your explanations are really good,Hope it grows :)

  • @ilikespaghetti2170
    @ilikespaghetti2170 5 лет назад +1

    can you make just like this one but if you click a button it will function the XML HTTP Requests

  • @robot67799
    @robot67799 2 года назад

    You deserve a subscribe. BTW which theme you are using in atom?

  • @rethpark
    @rethpark 4 года назад

    thank you man....

  • @fahadhafeez8086
    @fahadhafeez8086 4 года назад +1

    Great!

  • @nonetrix3066
    @nonetrix3066 2 года назад

    I've only used JSON until now one of the APIs I use supports both any reason to chose one over the other?

  • @shubhamarora547
    @shubhamarora547 6 лет назад +3

    gr8 video sir

  • @_productivity__nill_1131
    @_productivity__nill_1131 5 лет назад +2

    Good tutorial

  • @t3chnicolor
    @t3chnicolor 3 года назад

    ID on the keyboard, switches and keycaps you’re using?

  • @mackensonreginaldmichel399
    @mackensonreginaldmichel399 3 года назад

    Very good explanation. But I'd like to know when and why use it? Do we not need server-like languages anymore if we can use this? Thanks a bunch!!!

  • @omar7amdi
    @omar7amdi 3 года назад

    Thanks

  • @missinglink2416
    @missinglink2416 3 года назад

    what type of data can be returned by the xmlhttprequest object?

  • @davidtsiklauri8089
    @davidtsiklauri8089 5 лет назад +1

    Perfect :d

  • @jeel7373
    @jeel7373 2 года назад

    THANK YOU THANK YOU SO MUCH SIR 👑👑👑👑👑🇮🇳😀

    • @dcode-software
      @dcode-software  2 года назад

      You're welcome! Heads up - once you have learnt AJAX, I recommend using Fetch 🙂

  • @sergioa1113
    @sergioa1113 6 лет назад +3

    gracias! :D

  • @Meleeman011
    @Meleeman011 4 года назад

    For those having CORS issues, put the file on a local server such as a node.js server or nginx/apache server. I think node is easiest, you could run a dev server with node server.js and have server.js serve your file to localhost:3333 or whatever port, reference that in your xhr.request and as long and you have your development server running you can locally test without a CORS issue.

    • @gamersoul1098
      @gamersoul1098 3 года назад

      i saw one youtuber he was able to use file from local storage without cors error

    • @gamersoul1098
      @gamersoul1098 3 года назад

      so is there any solution for cors error with out node.js ?

  • @BlokeBritish
    @BlokeBritish 3 года назад +1

    hows it going my name is Don - that standard tone in every video is a killer haha

  • @slapmyfunkybass
    @slapmyfunkybass 5 лет назад

    Firstly thanks for the video. To me though, it seems more logical to put the xhr open and send lines before the function. I have to admit I’ve never seen it done this way. But wouldn’t it make more sense to make the request, send it, then run the function to check all is okay and display? Of course it doesn’t matter as the function can sit anywhere, but in terms of logical programming wouldn’t that way round make more sense?

  • @mackymichel8052
    @mackymichel8052 2 года назад

    Thanks a lot. But I'd like to know what the header is, plz.

  • @10a3asd
    @10a3asd 4 года назад

    I don't understand how the script you wrote knows where to send the GET request to. What if you were sending the GET request to a web service?

  • @pjmclenon
    @pjmclenon 4 года назад

    hello my API keeps retruning HTML and so i cant run my search box to call the API
    do you know how to solve this issue?
    in your code i get null and so i have to comment out responseType line and then i just get HTML code in the console
    this is a sample API call
    @t
    thxz for any help i been trying to solve this myself and online for 3 weeks
    Lisa
    does anyone know the fix for this?
    i followed the code for fetch from david ceccia but i still get only HTML
    and so i will also get cant parse error < because thats the 1st caracter of an HTML page

  • @scallywagin
    @scallywagin 3 года назад

    Hey guys, im getting this error "Uncaught TypeError: Cannot set property 'textContent' of null
    at XMLHttpRequest.xhr.onreadystatechange", any idea? I literally copied Dom's exercise............

  • @shiyoh-8289
    @shiyoh-8289 5 лет назад +3

    Thank you for sharing the video!
    I use same code as you said but I got error code related to CORS, it said "Access to XMLHttpRequest ...from origin 'null' has been blocked by CORS policy"

    • @shiyoh-8289
      @shiyoh-8289 5 лет назад +1

      and I found that I just forgot to add the if statement about "Readystate ==4"
      when i add it everything go well

    • @PratikSahu18
      @PratikSahu18 4 года назад

      AJAX only works in server , not in local directories

  • @mortenrobinson
    @mortenrobinson 3 года назад

    0:48 why? I thought you just said Ajax were for making requests to other websites, webservices and webpages? So you're going to use ajax to request a local text file from yourself? And it even has to be in the same directory? Where do the external websites, webservices and webpages enter the picture?

  • @intellsoft
    @intellsoft 4 года назад

    How do you send a XHR request to a Laravel controller and returns data to the view.

  • @Good-Enuff-Garage
    @Good-Enuff-Garage 2 года назад

    Failed to load response data. No data found for resource with given identifier.

  • @4u4000
    @4u4000 5 лет назад +4

    I ran the same code on my file system and i get a CORS error. How can i resolve this issue?

    • @minach69
      @minach69 5 лет назад

      I have the same issue

    • @malteeaser101
      @malteeaser101 5 лет назад

      I got that when trying to access a different site or sending a request to the local file system. I think it is a security thing, maybe.
      It works fine when requesting files from the server that the original HTML file came from.

    • @dcode-software
      @dcode-software  5 лет назад +2

      Generally speaking you can't fix this "issue" - it's a security feature enforced by the web browser. The reason it's happening is because you're trying to access a resource from a different origin (or site).
      If you'd like to get around this problem you need to have access to the web server which is providing the resource and set the Access-Control-Allow-Origin header accordingly.

    • @Meleeman011
      @Meleeman011 4 года назад

      put the file on a local server such as a node.js server or nginx/apache server. I think node is easiest, you could run a dev server with node server.js and have server.js serve your file to localhost:3333 or whatever port, reference that in your xhr.request and as long and you have your development server running you can locally test without a CORS issue.

  • @abdulhaqfayeq5962
    @abdulhaqfayeq5962 Год назад

    I wrote like this but , did work

  • @fahadhafeez8086
    @fahadhafeez8086 4 года назад

    Access to XMLHttpRequest at 'file:///D:/xampp/htdocs/dom.txt' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, brave, https.
    My console error!

  • @filmlife1000
    @filmlife1000 5 лет назад +1

    i keep getting this error:
    index.html:30 Access to XMLHttpRequest at 'file:/..../dom.txt' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
    Any idea what I'm doing wrong?

    • @hyperspace2223
      @hyperspace2223 3 года назад +1

      That’s because you’re using a file, you need nodejs

    • @filmlife1000
      @filmlife1000 3 года назад

      @@hyperspace2223 Thanks for the reply - can't believe I used this over a year ago! I've changed to Scala now - haven't touched JS in ages :0

    • @hyperspace2223
      @hyperspace2223 3 года назад

      @@filmlife1000 You're welcome :D

  • @pedrosoares7273
    @pedrosoares7273 4 года назад

    Very well explained, the most basic of basics. However, my chrome for some reason does not let me acess the files.
    " Access to XMLHttpRequest at 'file:///C:/XXX/XXXX/XXXXX/XXXX/dom.txt' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https."
    How to fix this?

    • @Meleeman011
      @Meleeman011 4 года назад

      put the file on a local server such as a node.js server or nginx/apache server. I think node is easiest, you could run a dev server with node server.js and have server.js serve your file to localhost:3333 or whatever port, reference that in your xhr.request and as long and you have your development server running you can locally test without a CORS issue.

  • @gokharol
    @gokharol 3 года назад

    Could I pass on an external URL for instance xhr.open('get', 'www.google.com',true) ?
    I'm looking forward to get elements by ID in real internet pages, do you think I need "CORS" first hand?

    • @gamersoul1098
      @gamersoul1098 3 года назад

      hiii could you solve cors error please tell me

  • @nilavdas_3234
    @nilavdas_3234 3 года назад

    Can we write the text file ?

  • @harsheenajunejo2981
    @harsheenajunejo2981 4 года назад

    Hi, How to store the XHR response as an array or JSON object by using javascript only. Can you please guide me on how to obtain that

    • @dcode-software
      @dcode-software  4 года назад

      I have plenty of videos on that, for example:
      ruclips.net/video/W6NsAO08vmE/видео.html

  • @domaincontroller
    @domaincontroller 3 года назад

    I've installed LAMP. Put the index.html and dom.txt in /var/www/html and removed all my chrome extensions which were creating websocket issues. Everything worked fine then.

  • @FordExplorer-rm6ew
    @FordExplorer-rm6ew 5 лет назад

    What are the 2 links at the top? What is RUclips.local? I've tried googling it but nothing's coming up conclusive

    • @dcode-software
      @dcode-software  5 лет назад

      It's setup locally:
      www.dummies.com/programming/networking/network-administration-the-hosts-file/

  • @md.imrulhasan8757
    @md.imrulhasan8757 4 года назад

    I can't set ajax code in my system..... it doesn't work.... How can I fix ?

  • @minach69
    @minach69 5 лет назад

    Hi, nice video. I did exactly what you did and I get the below error. What do you think is the issue?
    mexico.js:29 Access to XMLHttpRequest at 'file:///C:/Users/imedi3/Documents/Web%20Reports/Mexico%20Triggers/prueba.txt' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
    (anonymous) @ mexico.js:29

    • @dcode-software
      @dcode-software  5 лет назад

      Cross-origin. You need to make sure the file you're trying to read is on the same origin (or server) as the origin that the JavaScript is running on

  • @faizanmemon4956
    @faizanmemon4956 6 лет назад +1

    Thanks for a great video ! But please help me get rid of this : Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

    • @dcode-software
      @dcode-software  6 лет назад

      Thank you! For that issue, are you using the "file" protocol (reading from the disk directly rather than using a server)?

    • @faizanmemon4956
      @faizanmemon4956 6 лет назад

      Can i use localhost using xammp ?

    • @dcode-software
      @dcode-software  6 лет назад +1

      Yeah! That will be fine - I believe for purposes of security you are unable to access local files using JavaScript, because then any website would be able to read the local files on your computer

    • @faizanmemon4956
      @faizanmemon4956 6 лет назад +1

      Can i use files other than .txt ?

    • @dcode-software
      @dcode-software  6 лет назад +1

      Definitely, as far as I'm aware you can use whatever file you want, in fact you don't even need a file, as long as you have a HTTP response and some body content, you're OK

  • @souravdeb6126
    @souravdeb6126 5 лет назад

    What is the editor, please?

    • @dcode-software
      @dcode-software  5 лет назад +1

      The editor is Atom

    • @souravdeb6126
      @souravdeb6126 5 лет назад +1

      I put the question and went out for a walk hoping that when I come back, someone might have put the answer so thank you!!

  • @donaldklopper
    @donaldklopper 2 года назад

    Clever example :-D there's no web server needed ...

  • @glitchdigger
    @glitchdigger 2 года назад

    This didn't even remotely work for me. Very odd.

  •  4 года назад

    dude, you need a soft touch keyboard! or move the mic away from the keyboard... LOL thx for the video

  • @prasannaparthiban7050
    @prasannaparthiban7050 Год назад

    what is f*****in dada

  • @mrchedda
    @mrchedda 5 лет назад

    I'm trying to finish watching but your pretentious SUPER-DUPER tactile feedback , "I'm a real programmer", keyboard is annoying as $h!t3. You people think you're superior because your keyboard requires a million JOULES to type one letter. Terrible

  • @salexkorsan8790
    @salexkorsan8790 Год назад

    is there any way to get Outside result of the Function
    xmlhttp.onreadystatechange=function(){ } // i want result here Out side of the Function.
    trying to solve from two Month please help

  • @greatpandas99
    @greatpandas99 4 года назад +3

    You're a lifesaver man, I really appreciate this. Thanks