Phyrus: Send AJAX GET and POST requests to submit forms

Recommend this page to a friend!
  Info   Documentation   View files Files   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 94 All time: 425 This week: 21Up
Version License JavaScript version Categories
phyrus 1.0.2MIT/X Consortium ...5AJAX
Description 

Author

This object can send AJAX GET and POST requests to submit forms.

It can send AJAX requests to given URLs using POST or GET methods to the same domain or even cross domain requests.

The object can also submit forms and upload files via AJAX.

Picture of Yail A. Anderson
  Performance   Level  
Name: Yail A. Anderson <contact>
Classes: 1 package by
Country: Argentina Argentina
Age: 36
All time rank: 1652 in Argentina Argentina
Week rank: 6 Up1 in Argentina Argentina Up

Documentation

phyrus v1.0.1

A simple library to make ajax request with XMLHttpRequest 2 and upload files easily

Installation

Direct

  1. First download the phyrus.js file into your project
  2. Then you have to include it in your Html5 document
<!-- You have to put the Html5 doctype -->
<!DOCTYPE html>
<html>
  <!-- if you want compatibility with IE11 and less https://github.com/then/promise -->
  <!--[if lte IE 11]><!-->
  <script src="https://www.promisejs.org/polyfills/promise-6.1.0.js"></script>
  <!--<![endif]-->
  <!-- phyrus -->
  <script src="phyrus.js"></script>
</html>

With Bower

  bower install phyrus

<!-- You have to put the Html5 doctype -->
<!DOCTYPE html>
<html>
  <!-- if you want compatibility with IE11 and less https://github.com/then/promise -->
  <!--[if lte IE 11]><!-->
  <script src="lib/promise/index.js"></script>
   <!--<![endif]-->
  <!-- phyrus -->
  <script src="lib/phyrus/phyrus.js"></script>
</html>

Example


  // ajax request when a form is submited
  ///////////////////////////////////////
  var form = document.querySelector('#form');

  form.addEventListener('submit', function (e) {
      e.preventDefault();
      
      // also you could write it like this p.ajax() 
      phyrus.ajax('testPost.php') // url to which the request is made

          .request({
              // url: 'testPost.php', you can also put the url here
              method: 'POST',
              data: new FormData(e.target)
          })
          //success callback
          .then(function (response) {
              console.log(response);
          })
          //error callback
          .catch(function (response) {
              console.log(response);
          });

  });
  
  // when upload files with input type file
  //////////////////////////////////////////
  document.querySelector('input[type="file"]').addEventListener('change', function(e) {

     phyrus.uploadFiles(this.files)
       .into('test.php')
       .then(function (response) {
          console.log(response);
       })
       .catch(function(response){
          console.log(response)
       });

  }, false);
  
  // or we can do the same like this, if we put a 
  // data-uploadfiles attribute in the input type file
  ////////////////////////////////////////////////////
  phyrus.uploadFiles({
      success: function (e) {
          console.log("custom success callback");
      },
      //error
      error: function (e) {
          console.log("custom error callback");
      }
  }).into('test.php');
  
  // when we want to use drag and drop to upload files
  ////////////////////////////////////////////////////
  var dropzone = document.querySelector('.dropzone');

  // events listeners
  dropzone.addEventListener("dragover", function (e) {

      e.preventDefault(e);
      e.target.classList.add('drop-active');

  }, false);

  dropzone.addEventListener("dragenter", function (e) {

      //e.preventDefault(e);
      e.target.classList.add('drop-active');

  }, false);


  dropzone.addEventListener("dragleave", function (e) {

      //e.preventDefault(e);
      e.target.classList.remove('drop-active');
      e.target.classList.remove('drop-target');

      if(_isDefined(options.ondragleave) && _isFunction(options.ondragleave)){
          options.ondragleave(e);
      }
  }, false);

  dropzone.addEventListener("drop", function (event) {

      event.preventDefault();
      event.target.classList.remove('drop-active');

      // get the files
      var files = event.dataTransfer.files;


      phyrus.uploadFiles( files )
          // target
          .into('test.php')
          // promises
          .then(function (response) {
              console.log(response);
          })
          .catch(function(response){
              console.log(response)
          });

  }, false);


       



  Files folder image Files (5)  
File Role Description
Files folder imagecss (1 file)
Accessible without login Plain text file bower.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License
Plain text file phyrus.js Class Class source
Accessible without login Plain text file README.md Doc. Auxiliary data

  Files folder image Files (5)  /  css  
File Role Description
  Accessible without login Plain text file phyrus.css Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:94
This week:0
All time:425
This week:21Up