GithubHelp home page GithubHelp logo

ajaxrequest's Introduction

Ajax Request

This blog explain Ajax Request in Angular2, JQuery And Vanila JavaScript

#AngularJS

  1. then have 1st successCallback and 2nd errorCallback:

    $http({
       method: 'POST', 
       url: '/someUrl',
       headers: { 'Content-Type': dataType }, 
       data: { test: 'test' }
    }).then(function successCallback(response) {}, function errorCallback(response) {});
    
  2. Short cut:

    $http.get('/someUrl', config).then(successCallback, errorCallback); 
    $http.post('/someUrl', data, config).then(successCallback, errorCallback);
    
    $http.get('/someUrl', config).success(successCallback).error(errorCallback); 
    $http.post('/someUrl', data, config).success(successCallback).error(errorCallback);
    

    Post call have body as data.

  3. Abort Request:

#Jquery:

  1. then have 1st successCallback and 2nd errorCallback, and success property too have successCallback, which will call first.

    $.ajax({
       type: "POST",
       url: 'someUrl', 
       dataType: dataType , 
       data: data, 
       success: success1Callback
    })
    .done(function success2Callback() { alert( "second success" ); })
    .fail(function errorCallback() { alert( "error" ); })
    .always(function() { alert( "finished" ); });
    
  2. Short cut:

    $.get('/someUrl', config).done(successCallback).fail(errorCallback); 
    $.post('/someUrl', data, config).done(successCallback).fail(errorCallback);
    

    Post call have body as data.

  3. Abort Request:

    var request = $.ajax({
      type: "POST",
      url: 'someUrl', 
      dataType: dataType , 
      data: data, 
      success: success1Callback
    });
    request.done(function success2Callback() { alert( "second success" ); })
    request.fail(function errorCallback() { alert( "error" ); });
    
    request.abort();
    

#JavaScript

  1. SynchronousRequest:

    function httpGet(theUrl) { 
       var xmlHttp = new XMLHttpRequest(); 
       xmlHttp.open( "GET", theUrl, false); // false for synchronous request 
       xmlHttp.send( null ); 
       return xmlHttp.responseText; 
    }
    
  2. Asynchronous Request:

    function httpGetAsync(theUrl, callback) { 
       var xmlHttp = new XMLHttpRequest(); 
       xmlHttp.onreadystatechange = function() { 
             if (xmlHttp.readyState == 4 && xmlHttp.status == 200) callback(xmlHttp.responseText); 
       } 
       xmlHttp.open("GET", theUrl, true); // true for asynchronous 
       xmlHttp.send(null); 
    }
    

ajaxrequest's People

Contributors

amitthakkar avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.