What is the recommended practice to check if the jQuery Ajax function is present and recognized by the browser, does anyone know?

-1

What is the recommended practice to check if the jQuery Ajax function is present and recognized by the browser, does anyone know?

// forma 1
if (!$.ajax) {
  alert ('Status xhr: não suporta Ajax');
 return false;
}
// forma 2
if (typeof $.ajax !== 'function') {
  alert ('Status xhr: não suporta Ajax');
 return false;
}

In many of the examples on the Mozilla MDN site the condition for testing functions according to form 1 is well used,

link

    
asked by anonymous 15.01.2018 / 16:35

4 answers

3

Use jQuery.support.ajax or $.support.ajax

if (!$.support.ajax) {
    alert("Status xhr: não suporta Ajax");
}
    
15.01.2018 / 17:34
0

I got the code at link

    <script type="text/javascript"> 
      var xmlhttp; 
      function checkAJAXSupport() {
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
          xmlhttp= new XMLHttpRequest();
          alert("Yes. Your browser must be one among them - Mozilla, Safari, Chrome, Rockmelt, IE 8.0 or above");
        } else if (window.ActiveXObject) { // IE
          try {
            xmlhttp= new ActiveXObject("Msxml2.XMLHTTP");
            alert("Yes. Your browser must be IE");
          } 
          catch (e) {
            try {
              xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
              alert("Yes. Your browser must be IE");
            } 
            catch (e) {}
          }
        }  if (!xmlhttp) {
          alert("No. Giving up Cannot create an XMLHTTP instance. Your browser is outdated!");
          return false;
        } 
 } 
</script>
    
15.01.2018 / 16:44
0

You can use When in your request

    $.when( $.ajax ).then(function( data, textStatus, 
      jqXHR ) {
      alert( jqXHR.status ); // Alerts 200
    })
    .catch(function() {
      alert ('Status xhr: não suporta Ajax');
    })
    
15.01.2018 / 16:49
0

First you can create a variable to make a ajax request whatever

Dai inside the function you are testing the possible browsers, if not accept returns an error

    function ajaxFunction() {
      var xmlHttp;

      try {
       // Firefox, Opera 8.0+, Safari
       xmlHttp=new XMLHttpRequest();
      }
      catch (e) {
        // Internet Explorer
        try  {

           xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

        }
        catch (e) {
          try {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
          catch (e) {
             alert("Your browser does not support AJAX!");

             return false;
         }

    }
    
15.01.2018 / 20:26