Best Practices:

  1. When debugging AJAX code, make sure you activate inspect element, and watch both console and network panel.
  2. To process AJAX data, use the embedded success, done, and fail methods
  3. The $.ajax API is difficult to read, but the call does return jqxhr - jQuery XMLHTTPRequest object. This object has information (response, error, status, etc) about AJAX call.
  4. $.post( ... ), which is a shorthand version $.ajax( { type: "POST", url: urlString, ... } )
  5. Make sure you set the properties: url, type, datatype, contenttype and async, if not strange things will happen, defaults are always changing.
  6. jQuery has a lots of ways to call AJAX, but its best to stick with just $.ajax( javascript Array map )
  7. The biggest problem with a JavaScript arrays map is the extra set of braces and the corresponding callback functions can make reading the code very difficult. Add comments to make sure you know what are the matching braces. For example ), /* end of success */
  8. Call back methods and javascripts array maps can be hard to read, make sure you are using a good IDE.
  9. You can also use chaining off the initialal object, such as .fail( function() .. ), etc.
  10. Nota bene, jQuery updates frequently change names and drop properties, if you have only one type of AJAX call in all your programs it is a lot easier to update.
  11. When setting up links to jQuery CDNs, make sure each CDN on your site is identical. You want to be able to update all CDNS with either a single global file your load or with edit/replace.
Make sure you activate inspect element, and watch both console and network panel. AboutNumber.php is listed after table.
jquery Code - ajax api make Request Response - Status:
Collapse
     
      console.log( "Sending aboutNumber.php?number=7" );
      var xhrCallback;
      var jqxhr = $.ajax( { url : "aboutNumber.php?number=7",
      	type: "GET", /* GET is default */
        dataType: "xml",
        async: false,
        contentType: "text/xml; charset=\"utf-8\"",
        success : function( result, status, xhr )
        { xhrCallback = xhr; 
        console.log( "Result and xhr.responseXML are the same? " +
        (result === xhr.responseXML ? "Yes" : "No" ) ); },
        /* will use jqxhr to set data  success: function( result ) {
        $( "#success0" ).html( $( result ).find( "AboutNumbers" ) );
        } */ 
        /* end sucess function */
        } );
        /* note jqxhr.responseType is dependent on the dataType, result automatically set */
        console.log( "Got back a jq xmlhttprequest object: " + jqxhr  + 
        " Backward compatible " + jqxhr.responseXML + " xhr callback " + xhrCallback );
        console.log( "Return jqxhr and call xhr are the same? " 
      + ( jqxhr === xhrCallbck.responseXML ? "Yes" : "No" ) ) ;
      $( "#success0" ).html( $( jqxhr.responseXML ).find( "AboutNumbers" ) );
      $( "#status" ).text( "Status Code " + jqxhr.status + "  " +  jqxhr.statusText );
Uses the propety value resonseXML from either value jqxhr returned from $.ajax call or xhr third argument in success callback.

#success0
Collapse
$("#input1").click( function(  ) {
		//alert( "clicking sucessBtn "  );
		console.log( "Setting up successive ajax call to aboutNumber" +
     " .response#success1 should see response" );
		$.ajax( 
		{ url : "aboutNumber.php",
		  type: "GET", /* GET is default */
          dataType: "xml",
          async: false,
          contentType: "text/xml; charset=\"utf-8\"",
          data : { number : 3 },  /* can use url?number=3 also */
          success : function( result, status, xhr ) { 
			console.log( "xml result: " + result );
			console.log( "xhr.responseXML: " + xhr.responseXML ); 
			/* for some odd reason once you use xhr.respnseXML
            $result doesn't work (rewind or jQuery bug) */
			//$("#success0" ).html( $( xhr.responseXML ).find( "AboutNumbers" ) );
			$("#status").text( status );
			$("#success1").html(  $( result ).find( "AboutNumbers" )  );  
			},
		} ); //end $.ajax
	} ); //end button#successBtn
   
Using result argument of callback success:
#success1
Collapse
     	var num = $( "#input2Number" ).val();
		if( ! /\d+/.test( num ) ) {
			console.log( "Need integer this is not good:" + num  );
			return;
			}
		console.log( "Sending the integer: " + num );
		$.ajax( 
		{ url : "aboutNumber.php",
		  data : { number : num },
         

#success2
Collapse
Adding the error javaScript map, console.log used to show errors, also check Network tab
error : function( xhr, status, error ) { console.log( "error " + error); $("#status").text( status ); $("#success4").html( error ); },
Will Display 404 not found error:
#success3
Collapse
      Basic CORS error
$( "#input4" ).click( function( ) { uri = "https://cim.saddleback.edu/~mmouse0/aboutNumber/aboutNumber.php"; console.log( "File " + uri + " does not exists" ); $.ajax( { url : uri, type: "GET", /* GET is default */ dataType: "xml", async: false, crossDomain: true, /* default is false */ contentType: "text/xml; charset=\"utf-8\"", data : { number : $("#input4Number").val() }, /* can usr url?number=3 also */ success : function( result, status, xhr ) { console.log( "xml success: " + result ); console.log( "xhr: " + xhr.responseText ); $("#status").text( status ); $("#success4").html( error ); }, error : function( xhr, status, error ) { console.log( "error " + error); $("#status").text( status ); $("#error4").html( error ); }, } ); //end .ajax
#success4
#error4
<?php

  $xmlStr = "<?xml version='1.0' encoding='UTF-8'?>\n".
          /* "<?xml DOCTYPE AboutNumbers SYSTEM  'aboutNumber.dtd' ?>\n".
          '<?xml-stylesheet href="aboutNumber.css" ?>'. */
          '<AboutNumbers></AboutNumbers>';
  $xml = new SimpleXMLElement( $xmlStr );
   //generated using a Java Prime Sieve - didn't want to execute code on server for each request
   //results will be built into a database in the future
  $germainPrime = array( 2, 3, 5, 11, 23, 29, 41, 53, 83, 89, 113, 131, 173, 179, 191, 233, 239, 251, 281, 293,
       359, 419, 431, 443, 491, 509, 593, 641, 653, 659, 683, 719, 743, 761, 809, 911, 953, 1013, 1019, 1031,
       1049, 1103, 1223, 1229, 1289, 1409, 1439, 1451, 1481, 1499, 1511, 1559, 1583, 1601, 1733, 1811, 1889,
       1901, 1931, 1973, 2003, 2039, 2063, 2069, 2129, 2141, 2273, 2339, 2351, 2393, 2399, 2459, 2543, 2549,
       2693, 2699, 2741, 2753, 2819, 2903, 2939, 2963, 2969, 3023, 3299, 3329, 3359, 3389, 3413, 3449, 3491,
       3539, 3593, 3623, 3761, 3779, 3803, 3821, 3851, 3863, 3911, 4019, 4073, 4211, 4271, 4349, 4373, 4391,
       4409, 4481, 4733, 4793, 4871, 4919, 4943, 5003, 5039, 5051, 5081, 5171, 5231, 5279, 5303, 5333, 5399,
       5441, 5501, 5639, 5711, 5741, 5849, 5903, 6053, 6101, 6113, 6131, 6173, 6263, 6269, 6323, 6329, 6449,
       6491, 6521, 6551, 6563, 6581, 6761, 6899, 6983, 7043, 7079, 7103, 7121, 7151, 7193, 7211, 7349, 7433,
       7541, 7643, 7649, 7691, 7823, 7841, 7883, 7901, 8069, 8093, 8111, 8243, 8273, 8513, 8663, 8693, 8741,
       8951, 8969, 9029, 9059, 9221, 9293, 9371, 9419, 9473, 9479, 9539, 9629, 9689, 9791, 10061, 10091, 10163,
       10253, 10271, 10313, 10331, 10529, 10589, 10613, 10691, 10709, 10733, 10781, 10799, 10883, 11171, 11321,
       11369, 11393, 11471, 11519, 11549, 11579, 11699, 11783, 11801, 11813, 11831, 11909, 11939, 12011, 12041,
       12101, 12119, 12203, 12263, 12329, 12653, 12671, 12791, 12821, 12899, 12923, 12959, 13001, 13049, 13229,
       13313, 13451, 13463, 13553, 13619, 13649, 13763, 13883, 13901, 13913, 14009, 14081, 14153, 14159, 14249,
       14303, 14321, 14489, 14561, 14621, 14669, 14699, 14741, 14783, 14831, 14879, 14939, 15101, 15161, 15173,
       15233, 15269, 15401, 15569, 15629, 15773, 15791, 15803, 15923, 16001, 16091, 16253, 16301, 16421, 16493,
       16553, 16673, 16811, 16823, 16883, 16931, 17159, 17183, 17291, 17333, 17351, 17579, 17669, 17681, 17939,
       17981, 18041, 18131, 18149, 18191, 18233, 18341, 18443, 18461, 18731, 18773, 18803, 18899, 19163, 19301,
       19319, 19373, 19391, 19433, 19553, 19559, 19661, 19709, 19751, 19889, 19913, 19919, 19991, 20063, 20249,
       20369, 20393, 20411, 20441, 20693, 20753, 20759, 20771, 20789, 20879, 20921, 20939, 20963, 21011, 21089,
       21149, 21179, 21221, 21341, 21383, 21419, 21611, 21701, 21713, 21803, 21893, 22013, 22079, 22133, 22259,
       22271, 22343, 22349, 22409, 22433, 22469, 22481, 22541, 22751, 22853, 22943, 23099, 23279, 23321, 23339,
       23459, 23561, 23603, 23669, 23753, 23819, 23909, 23981, 24203, 24239, 24281, 24473, 24509, 24551, 24611,
       24683, 24749, 24971, 25073, 25229, 25523, 25601, 25643, 25673, 25703, 25799, 25841, 25913, 25919, 26111,
       26189, 26459, 26501, 26573, 26633, 26849, 26879, 26891, 26993, 27143, 27281, 27479, 27539, 27551, 27581,
       27743, 27773, 27809, 27893, 27983, 28001, 28019, 28403, 28499, 28559, 28571, 28643, 28751, 28793, 28859,
       28901, 28949, 28961, 29021, 29033, 29201, 29339, 29363, 29453, 29483, 29531, 29723, 29873, 30269, 30323,
       30389, 30449, 30671, 30689, 30773, 30851, 30983, 31019, 31151, 31253, 31319, 31469, 31649, 31721, 31793,
       31799, 31859, 32003, 32009, 32141, 32159, 32381, 32531, 32561, 32573, 32633 );

        $fermat = array( 3, 5, 17, 257, 65537, 2147483647, 2147483647, 2147483647);

    //couldn't find a built in binary Search
    function binarySearch( $needle, $haystack )
    {
        $needle = (integer)$needle;
        $low = 0; $high = count( $haystack );
        while( $low <= $high ) {
            $mid = (integer)( ($low+$high)/2 );
            if( $haystack[ $mid ] == $needle ) return $mid;
            elseif( $needle > $haystack[$mid] ) $low = $mid+1;
            else $high = $mid-1;
        }
        return -$mid;
    }

    if( array_key_exists( "number", $_GET ) ) $n = $_GET[ 'number' ];
    elseif( array_key_exists( "number", $_POST ) ) $n = $_POST[ 'number' ];
    else $n = 3;
    $n = (integer)$n;

    $child = $xml->addChild( "AboutNumber" );
    $child->addChild( "Number", $n );
    $nInfo = $child->addChild( "NumberInfo", (binarySearch( $n, $germainPrime )>=0?"true":"false") );
    $nInfo->addAttribute( "description", "Sophie Germain Prime, is a number n whereby both n, and 2*n+1 are prime" );
    $nInfo->addAttribute( "title", "germainPrime" );

    $nInfo = $child->addChild( "NumberInfo", (binarySearch( $n, $fermat )>=0?"true":"false") );
    $nInfo->addAttribute( "description", "Fermat 2^2^n + 1  its believed all Fermats are prime" );
    $nInfo->addAttribute( "title", "fermat" );


    header( 'Access-Control-Allow-Origin: *' );
    header( 'Content-type: text/xml' );
    echo $xml->asXML();

    /*
    echo binarySearch( 5, $germainPrime )."\n";
    echo binarySearch( 17, $fermat );
    echo binarySearch( 20, $fermat ); */
?>