Sending POST data with AJAX in JavaScript

Поделиться
HTML-код
  • Опубликовано: 2 окт 2024

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

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

    I've also made a video on sending JSON in similar fashion, check it out below!
    ruclips.net/video/mNrJDGfQGz0/видео.html

  • @scheroone
    @scheroone 5 лет назад +83

    finally someone who does it without jquery, thanks a lot

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

      No worries, I like to avoid jQuery in all my JavaScript tutorials

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

      @@dcode-software How come?

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

      Same here! I will probably do it with jQuery in the future, but I don't want to use that method until I learn how it works and know what exactly my code does.

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

      soo true

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

      Because JQuery is merely a library and there are libraries coming out every month, at some point JQuery will lose it's place and are you gonna stick to a library for life instead of the language itself?

  • @mukeshroy4971
    @mukeshroy4971 6 лет назад +29

    Only you provide Ajax post request without using jquery. Thanks a lot...

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

      No worries, in a lot of cases you don't need jQuery just to perform a HTTP Request 😁

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

      @@dcode-software this is the best tutorial so far. me personally dont like jquery its like bootstrap vs css grid..

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

    Thank you so much, this was very helpful. I was having trouble doing this, It's 4:00am and I was really struggling thank you so much.

  • @chrisrockscode1202
    @chrisrockscode1202 11 месяцев назад

    I swear if this just helped me with y project, you got a subscriber

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

    this is so much fuking better than other tutorials out there that GETs data from an arbitrary .txt file or some magical JSON file that pops up out of no where.

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

    Very helpful. Thank you. To bring this up-to-date, it's better to use xhr.responseType to set the data format and then xhr.response instead of the older xhr.responseText.

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

    Could you tell me how to do this with node instead of php?

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

    Finally found someone who didnt used stupid jquery
    i dont mean to hate on people or the library but it can just die i dont even care about it, vanilla maybe takes longer to write but its easier to debugg and access.

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

    thanks this helped me

  • @marineoutsource
    @marineoutsource 4 года назад +4

    I've included this in all my playlist just to make sure I will not lose this tutorial :)

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

    frickin light theme 😒

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

    WTF!!! Horrible example for the use of XHMHttpRequest and demonstration of the "xhr.send" function syntax!!!

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

    how to keep values in form after eror in validation and after no errors redirect

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

    hi,what if I want to determine what the button submits ,say I have 3 buttons with IDs fruit1,forwad and show ,when you click on fruit1 the infomation in show (an image and image name) must be put in the button with id "forwad" on the same page.
    I Tried this but can't get anything
    function myonclickfn(){
    document.getElementById('fruit1').onclick=function(){
    $.ajax({
    type: "POST",
    url: $("#forwad"),
    data: $("#show"),
    });
    };
    } ;

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

    Thank you very much. I can't understand how you decide the header name("Content-Type") and the value("application/x-www-form-urlencoded") in setRequestHeader() method.

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

    How do I use this to pass a variable that already carries a value and not give the value on the xhr.send ? and instead of I'm displaying it to the value of an but I don't know how , any help is well appreciated

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

    What if the php file was uploaded to another website will the post request work?

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

    I've tried it and it works. Thank you so much. What materials or books did you read to be able to understand this yourself? And how long did it take you?

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

      No problem, I didn't read any books. I just read examples online and look at the MDN documentation. As always with programming, you only learn if you actually use these tools regularly

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

    I forgot the request header and I was wondering what's wrong. Now I Know why my data wasn't going anywhere thanks

  • @alexisaddicted
    @alexisaddicted 6 лет назад +4

    Awesome video! You could be more specific also in showing how to send input value though!

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

      Hey mate, thank you and yeah I'm still working on my presentation skills 😁

    • @1playfair
      @1playfair 4 года назад

      I’m grateful for what I got. So thanks brother

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

    How did ur local host is able to handle requests. Do u have any http server setup on ur local machine?

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

    Hi, I have a "405 Method Not Allowed" ANY HELP PLS??

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

    Why don't you give the source code in description

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

    Oh man, that is the best sounding keyboard on all of youtube!

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

    Great video! But please use dark theme next time, this shiny white calcine my retina.

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

      No worries and yeah if you check out my recent videos they all have a dark theme, most people prefer dark over light

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

    Is xmlhttprequest is ajax?

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

    thanks is very helpful

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

    outstanding - thank you. you don't need to use jquery if you were just going to use it for AJAX. this video walks through exactly how to use normal javascript to do AJAX. thank you!

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

    The ajax returns the error - {"readyState":0,"responseText":"","status"=0,"statusText"="error"}. Guess it implies its unable to reach the PHP file? Any solution?

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

    405 Method not allowed, I followed exaxtly the same step. Anybody knows how fix that?

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

      If you're using a built-in development web server via the code editor then you'll need to switch to an actual web server like Apache for example.

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

    thank you

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

    Not working for me :(

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

    What if the php file was uploaded to another website will the post request work?

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

    Everybody where saying jQuery was way easier than vanilla js but this video was so clear and easy to understand. Thanks!!

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

    I'm still a bit confused. Where's the destination set in the code? Isn't the dom.php the source file we're sending? Where can I set the destination folder I wanna send the file to? Thx!

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

    Is it the same process when we want to send mail, and how can we get feedback on whether the mail, was sent, or an error occurred? Do you have such a tutorial? By the way, this video is great, congratulations, keep it up.

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

    Than q

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

    I made a website that request 2 numbers from input and send them to the server and send the summary to the client but I got an error message "Undefined Index" what can I do?

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

    how did you setup vscode for web development? what extensions are you using?

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

    Great

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

    so my html shows status 200 with a green circle yet my PHP is receiving no data and $_POST is empty?

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

    hello. Nice tutorial u got there. How do I send base64 without urlencoding?

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

    message: Array .
    UserName: Array .
    Password: Array .
    First Name: Array .
    Middle Name: Array .
    Last Name: Array .
    i am having this problem how can fix it

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

    do i have to create a php file??

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

    Awesome! I love it how you use plain javascript instead of 'that' outdated library. A suggestion: could you make a video on how to include csrf tokens in the XMLHttpRequest header and how to avoid CORS errors? I was trying to send POST request to a rest api I quickly made with django rest framework but I'm having trouble circumventing csrf and cors protection.

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

    can you do this with javascript and c++

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

    OMG you explained this so well, I am using AJAX for the first time.... this really made a lot of sense! thanks.

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

    I have seen an Ajax example in a web development book by Robin Nixon but instead he uses var, is there a particular reason you chose to use const?

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

      Const is for constants, "variables" that do not have their value changed. His code will still work but if the value does not change it is much clearer to use a constant.

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

    I'm getting a http code 404. Both javascript & php files are in the same directory (wordpress). Before form submission xht.open("get","test.php", true); fails to find the php file.

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

      It looks to be something related to your routes or URL rewrite

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

    Thank you very much, its been a great help :)

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

    Ur the man, Dom 😁

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

    Easy, nice, planned, and clear explanation. Well done pro!

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

    Simple and neat explanation , Thanks

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

    Will it work if i do in json file instead of php

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

      Submitting to a JSON file won't work, you need to submit to an endpoint on the server side. In the case of PHP, it needs to be a PHP file. Laravel uses routes

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

      @@dcode-software ooh i see, thanks

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

    Simple and understandable, thank you!

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

    PHP worked perfectly. The folowing pure javascript + ajax request, returns the error function results -
    $.ajax({
    url: 'url' // URL of PHP file on pvt webserver which is successfully displaying JSON output,
    method: 'GET',
    dataType:'JSON',
    success: function(data){
    //success - elusive
    },
    error: function(error){
    //failure - returns this code
    }
    });

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

      Can you please send through the whole code?

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

      dcode Thanks for the response. The code is as follows -
      $.ajax({
      url: 'xxx.000webhostapp.com/xxxxxx.php', //this link returns JSON output successfully from the Twitter API 1.1 user_timeline code
      method: 'GET',
      dataType:'JSON',
      success: function(data){
      alert('Success');
      },
      error: function(error){
      alert('Error'); //This is the result I get
      }
      });

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

    Can you make another tutorial on XHR POST explaining how to send passwords/forms to PHP and PHP will send that to a SQL database(Optional) and talk about a little bit of security? Then also how to GET data from PHP. Almost like a one-page login system without page refresh. I, we really need this. There are no good videos on youtube about this. There are some, but not fully explained, used jQuery etc etc. Please.

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

      Thanks for the suggestion! I'd like to do PHP at some point, what do you mean by sending passwords/forms to a database? Do you mean a login page?

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

      Yes. A login page(or something like that). We'll give the credentials, Javascript will POST it(to PHP or whatever), then get a response from the server, tell us if the credentials are right. or login successfully.
      Typically when we submit a form, it redirects to another page(because of the action attribute) But in this way we can login in the same page without page refresh. We can preventDeafult action(submitting) with JS and send/receive information with JS. We stay at the same page.
      Messaging app or commenting works like this, even though these are not forms.
      Because this topic isn't about PHP, you can just show us the JS stuffs without much PHP explanation. You already have a video like that "Form Data (FormData) in Javascript (with AJAX)". Close enough!
      Thanks for replying. Your channel has a future I guess.

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

      ruclips.net/video/zvt8ff3d63Q/видео.html
      I made a video similar to this topic, try this one mate

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

      Whoa! How come I didn't noticed that video, That's why my mother used to say I can't see even though I have eyes.
      Thanks. Not just from me but also from the others who forgot to thank you.

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

      No problem, I hope it helped you 😁

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

    Yes but is there a way to output a javascript variable to a text file using ajax?

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

      Of course but the server side code will need to write to the text file

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

    HELP! the xhr.send(); method doesn't work, it gives error" 405 method is not allowed" i am trying it on a localhost server

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

      Are you using a webserver provided by one of your text editor plugins? If so, that is why. I recommend using something like XAMPP or WAMP as a server

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

      @@dcode-software the problems was the php page that i was linking to, the php function had an error

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

    hi nice tutorial, but how about sending an array data on it?

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

      Hey there, what you could do is serialize the array as JSON using the JSON.stringify method and then send it as the usual key-valye pair. Although on the receiving end you will need to parse the JSON string back into a native data structure. If using PHP, try json_decode

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

      oh okay, thank you. I'll try

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

    thank !!!!!

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

    Great video, but can you please explain how we send an array using the same method?

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

      I've made a video on this, here it is: ruclips.net/video/ycmci-hiPfQ/видео.html
      Just send an array instead of an object

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

    Pls teach us how to coding a chat system!

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

    I have a web page with dynamic forms on it generated by php. But the script only selects the first form. How do I make it select the forms dynamically?
    Please help me.

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

      Hi, what do you mean when you say "the script only selects the first form"?

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

      I want to submit the forms dynamically with Ajax but when I send the Ajax request it submits the values of the first form on the page even if I click submit for the last form. How do I make Ajax choose the specific form I submitted and send the request for it. Thanks.

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

      A Jasper If you're using AJAX then you should be able to specify what data you want to send with the request to the server - what you can do is get the value of each input element and send to through with the request

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

      Ok let me explain well.
      I have a php webpage that loops out posts from my database and foreach post it also loops out a form.
      Now if I load the page and submit the form data for the second post looped out Ajax will send the data of the first post's form. Same for all the post's forms on the page.
      How do I make Ajax choose the data that belongs to each post form and submit it instead of only submitting the data of the first post form everytime I send the request.

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

      Hey can you post the code to GitHub?

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

    How to do the same thing with JSON?

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

      Hey, I've got a video lined up for sending JSON via AJAX

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

      Hey mate, I've made that video if you're still interested :)
      ruclips.net/video/mNrJDGfQGz0/видео.html

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

    why is he on light mode ????????😒

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

      He switches to dark themes in future videos