Advertisement
terorama

Ajax snippets

Oct 15th, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 23.00 KB | None | 0 0
  1.    
  2.  
  3.                $.ajax ( {
  4.                type: 'POST',
  5.                url:  '<?=$_SERVER['PHP_SELF']?>',
  6.                dataType: 'json',
  7.                data: {step:''}
  8.             }
  9.            
  10.             ).done(
  11.                function(infj) {
  12.                  
  13.                   $.each(infj, function(i,valu) {
  14.                      var zDiv=$('<div>',{'class':'img_div'});
  15.                      var zImg=$('<img>',{'src':valu['small'],'rel':valu['big']});
  16.                            
  17.                      //--------------------------------
  18.                      zDiv.append(zImg);
  19.                      $('#allpics').find('#zclose').before(zDiv);
  20.                      
  21.                   });
  22.  
  23. //----------------------------------------------------------------------------------------------
  24.                 $.ajax ( {
  25.                type: 'POST',
  26.                url:  '<?=$_SERVER['PHP_SELF']?>',
  27.                data: {step:'', favpic:inpic}
  28.             }).done(
  29.                function(html) {
  30.                   //alert(html);
  31.                });
  32.  
  33.  
  34. //----------------------------------------------------------------------------------------------
  35. $.ajax({
  36.   url: "test.html",
  37.   context: document.body
  38. }).done(function() {
  39.   $(this).addClass("done");
  40. });
  41. //--------------------------------------------
  42. $.ajax({
  43.   statusCode: {
  44.     404: function() {
  45.       alert("page not found");
  46.     }
  47.   }
  48. });
  49. //--------------------------------------------
  50. $.ajax({
  51.    url: a_cross_domain_url,
  52.    xhrFields: {
  53.       withCredentials: true
  54.    }
  55. });
  56.  
  57. //--------------------------------------------
  58. $.ajax({
  59.   url: "http://fiddle.jshell.net/favicon.png",
  60.   beforeSend: function ( xhr ) {
  61.     xhr.overrideMimeType("text/plain; charset=x-user-defined");
  62.   }
  63. }).done(function ( data ) {
  64.   if( console && console.log ) {
  65.     console.log("Sample of data:", data.slice(0, 100));
  66.   }
  67. });
  68. //--------------------------------------------
  69.  
  70.  
  71. var jqxhr = $.ajax( "example.php" )
  72.     .done(function() { alert("success"); })
  73.     .fail(function() { alert("error"); })
  74.     .always(function() { alert("complete"); });
  75.  
  76. // perform other work here ...
  77.  
  78. // Set another completion function for the request above
  79. jqxhr.always(function() { alert("second complete"); });
  80.  
  81. //--------------------------------------------
  82.  
  83. $.ajax({
  84.   url: 'ajax/test.html',
  85.   success: function(data) {
  86.     $('.result').html(data);
  87.     alert('Load was performed.');
  88.   }
  89. });
  90.  
  91.  
  92. //--------------------------------------------
  93.  
  94. var _super = jQuery.ajaxSettings.xhr;
  95.  
  96. jQuery.ajaxSettings.xhr = function () {
  97.     var xhr = _super(),
  98.         getAllResponseHeaders = xhr.getAllResponseHeaders;
  99.  
  100.     xhr.getAllResponseHeaders = function () {
  101.         if ( getAllResponseHeaders() ) {
  102.             return getAllResponseHeaders();
  103.         }
  104.         var allHeaders = "";
  105.         $( ["Cache-Control", "Content-Language", "Content-Type",
  106.                 "Expires", "Last-Modified", "Pragma"] ).each(function (i, header_name) {
  107.  
  108.             if ( xhr.getResponseHeader( header_name ) ) {
  109.                 allHeaders += header_name + ": " + xhr.getResponseHeader( header_name ) + "\n";
  110.             }
  111.             return allHeaders;
  112.         });
  113.     };
  114.     return xhr;
  115. };
  116. //--------------------------------------------
  117.  
  118. $.ajax({
  119.   type: "POST",
  120.   url: "some.php",
  121.   data: { name: "John", location: "Boston" }
  122. }).done(function( msg ) {
  123.   alert( "Data Saved: " + msg );
  124. });
  125.  
  126. //--------------------------------------------
  127.  
  128. $.ajax({
  129.   url: "test.html",
  130.   cache: false
  131. }).done(function( html ) {
  132.   $("#results").append(html);
  133. });
  134.  
  135. //--------------------------------------------
  136.  
  137. var xmlDocument = [create xml document];
  138. var xmlRequest = $.ajax({
  139.   url: "page.php",
  140.   processData: false,
  141.   data: xmlDocument
  142. });
  143.  
  144. xmlRequest.done(handleResponse);
  145.  
  146. //--------------------------------------------
  147.  
  148. var menuId = $("ul.nav").first().attr("id");
  149. var request = $.ajax({
  150.   url: "script.php",
  151.   type: "POST",
  152.   data: {id : menuId},
  153.   dataType: "html"
  154. });
  155.  
  156. request.done(function(msg) {
  157.   $("#log").html( msg );
  158. });
  159.  
  160. request.fail(function(jqXHR, textStatus) {
  161.   alert( "Request failed: " + textStatus );
  162. });
  163.  
  164. //--------------------------------------------
  165.  
  166. $.ajax({
  167.   type: "GET",
  168.   url: "test.js",
  169.   dataType: "script"
  170. });
  171.  
  172. //--------------------------------------------
  173. $.ajaxSetup({
  174.   url: 'ping.php'
  175. });
  176.  
  177. $.ajax({
  178.   // url not set here; uses ping.php
  179.   data: {'name': 'Dan'}
  180. });
  181. //--------------------------------------------
  182.  
  183. $.ajaxSetup({
  184.    url: "/xmlhttp/",
  185.    global: false,
  186.    type: "POST"
  187.  
  188.  });
  189.  $.ajax({ data: myData });
  190.  
  191. //--------------------------------------------
  192. $.ajax({
  193.   type: 'POST',
  194.   url: url,
  195.   data: data,
  196.   success: success,
  197.   dataType: dataType
  198. });
  199.  
  200. //--------------------------------------------
  201.  
  202. $.post('ajax/test.html', function(data) {
  203.   $('.result').html(data);
  204. });
  205.  
  206. //--------------------------------------------
  207.  
  208. // Assign handlers immediately after making the request,
  209.     // and remember the jqxhr object for this request
  210.     var jqxhr = $.post("example.php", function() {
  211.       alert("success");
  212.     })
  213.     .success(function() { alert("second success"); })
  214.     .error(function() { alert("error"); })
  215.     .complete(function() { alert("complete"); });
  216.  
  217.     // perform other work here ...
  218.  
  219.     // Set another completion function for the request above
  220.     jqxhr.complete(function(){ alert("second complete"); });
  221.  
  222. //--------------------------------------------
  223.  
  224. $.post("test.php");
  225.  
  226. $.post("test.php", { name: "John", time: "2pm" } );
  227.  
  228. $.post("test.php", { 'choices[]': ["Jon", "Susan"] });
  229.  
  230. $.post("test.php", $("#testform").serialize());
  231.  
  232. $.post("test.php", function(data) {
  233.    alert("Data Loaded: " + data);
  234.  });
  235.  
  236. $.post("test.php", { name: "John", time: "2pm" },
  237.    function(data) {
  238.      alert("Data Loaded: " + data);
  239.    });
  240.  
  241.  
  242. $.post("test.php", { name: "John", time: "2pm" },
  243.  function(data) {
  244.    process(data);
  245.  },
  246.  "xml"
  247. );
  248.  
  249. $.post("test.php", { "func": "getNameAndTime" },
  250.  function(data){
  251.    console.log(data.name); // John
  252.    console.log(data.time); //  2pm
  253.  }, "json");
  254.  
  255.  
  256. //--------------------------------------------
  257.  
  258. <!DOCTYPE html>
  259. <html>
  260. <head>
  261.   <script src="http://code.jquery.com/jquery-latest.js"></script>
  262. </head>
  263. <body>
  264.   <form action="/" id="searchForm">
  265.    <input type="text" name="s" placeholder="Search..." />
  266.    <input type="submit" value="Search" />
  267.   </form>
  268.   <!-- the result of the search will be rendered inside this div -->
  269.   <div id="result"></div>
  270.  
  271. <script>
  272.   /* attach a submit handler to the form */
  273.   $("#searchForm").submit(function(event) {
  274.  
  275.     /* stop form from submitting normally */
  276.     event.preventDefault();
  277.        
  278.     /* get some values from elements on the page: */
  279.     var $form = $( this ),
  280.         term = $form.find( 'input[name="s"]' ).val(),
  281.         url = $form.attr( 'action' );
  282.  
  283.     /* Send the data using post and put the results in a div */
  284.     $.post( url, { s: term },
  285.       function( data ) {
  286.           var content = $( data ).find( '#content' );
  287.           $( "#result" ).empty().append( content );
  288.       }
  289.     );
  290.   });
  291. </script>
  292.  
  293. </body>
  294. </html>
  295.  
  296. //--------------------------------------------
  297.  
  298. $.ajax({
  299.   url: url,
  300.   data: data,
  301.   success: success,
  302.   dataType: dataType
  303. });
  304.  
  305. //--------------------------------------------
  306.  
  307. $.get('ajax/test.html', function(data) {
  308.   $('.result').html(data);
  309.   alert('Load was performed.');
  310. });
  311.  
  312.  
  313. //--------------------------------------------
  314.  
  315. // Assign handlers immediately after making the request,
  316.   // and remember the jqxhr object for this request
  317.   var jqxhr = $.get("example.php", function() {
  318.     alert("success");
  319.   })
  320.   .success(function() { alert("second success"); })
  321.   .error(function() { alert("error"); })
  322.   .complete(function() { alert("complete"); });
  323.  
  324.   // perform other work here ...
  325.  
  326.   // Set another completion function for the request above
  327.   jqxhr.complete(function(){ alert("second complete"); });
  328.  
  329. //--------------------------------------------
  330.  
  331. $.get("test.php");
  332.  
  333. $.get("test.php", { name: "John", time: "2pm" } );
  334.  
  335. $.get("test.php", { 'choices[]': ["Jon", "Susan"]} );
  336.  
  337. $.get("test.php", function(data){
  338. alert("Data Loaded: " + data);
  339. });
  340.  
  341. $.get("test.cgi", { name: "John", time: "2pm" },
  342.    function(data){
  343.      alert("Data Loaded: " + data);
  344.    });
  345.  
  346.  
  347. $.get("test.php",
  348.    function(data){
  349.      $('body').append( "Name: " + data.name ) // John
  350.               .append( "Time: " + data.time ); //  2pm
  351.    }, "json");
  352.  
  353.  
  354. //--------------------------------------------
  355.  
  356. $('#result').load('ajax/test.html');
  357.  
  358. $('#result').load('ajax/test.html', function() {
  359.   alert('Load was performed.');
  360. });
  361.  
  362. $('#result').load('ajax/test.html #container');
  363.  
  364. $('#a').load('article.html');
  365.  
  366. $('#b').load('article.html #target');
  367.  
  368.  
  369. //--------------------------------------------
  370.  
  371. <!DOCTYPE html>
  372. <html>
  373. <head>
  374.   <style>
  375.  body{ font-size: 12px; font-family: Arial; }
  376.  </style>
  377.   <script src="http://code.jquery.com/jquery-latest.js"></script>
  378. </head>
  379. <body>
  380.  
  381. <b>Footer navigation:</b>
  382. <ol id="new-nav"></ol>
  383.  
  384. <script>
  385.   $("#new-nav").load("/ #jq-footerNavigation li");
  386. </script>
  387.  
  388. </body>
  389. </html>
  390.  
  391. //--------------------------------------------
  392.  
  393. <!DOCTYPE html>
  394. <html>
  395. <head>
  396.   <style>
  397.   body{ font-size: 12px; font-family: Arial; }
  398.   </style>
  399.   <script src="http://code.jquery.com/jquery-latest.js"></script>
  400. </head>
  401. <body>
  402.  
  403. <b>Successful Response (should be blank):</b>
  404. <div id="success"></div>
  405. <b>Error Response:</b>
  406. <div id="error"></div>
  407.  
  408. <script>
  409. $("#success").load("/not-here.php", function(response, status, xhr) {
  410.   if (status == "error") {
  411.     var msg = "Sorry but there was an error: ";
  412.     $("#error").html(msg + xhr.status + " " + xhr.statusText);
  413.   }
  414. });
  415.   </script>
  416.  
  417. </body>
  418. </html>
  419.  
  420. //--------------------------------------------
  421.  
  422. $("#feeds").load("feeds.html");
  423.  
  424. $("#objectID").load("test.php", { 'choices[]': ["Jon", "Susan"] } );
  425.  
  426.  
  427. $("#feeds").load("feeds.php", {limit: 25}, function(){
  428. alert("The last 25 entries in the feed have been loaded");
  429. });
  430.  
  431.  
  432. //--------------------------------------------
  433.  
  434. $.ajax({
  435.   url: url,
  436.   dataType: 'json',
  437.   data: data,
  438.   success: callback
  439. });
  440.  
  441. //--------------------------------------------
  442.  
  443. $.getJSON('ajax/test.json', function(data) {
  444.   var items = [];
  445.  
  446.   $.each(data, function(key, val) {
  447.     items.push('<li id="' + key + '">' + val + '</li>');
  448.   });
  449.  
  450.   $('<ul/>', {
  451.     'class': 'my-new-list',
  452.     html: items.join('')
  453.   }).appendTo('body');
  454. });
  455.  
  456. //--------------------------------------------
  457.  
  458. // Assign handlers immediately after making the request,
  459. // and remember the jqxhr object for this request
  460. var jqxhr = $.getJSON("example.json", function() {
  461.   alert("success");
  462. })
  463. .success(function() { alert("second success"); })
  464. .error(function() { alert("error"); })
  465. .complete(function() { alert("complete"); });
  466.  
  467. // perform other work here ...
  468.  
  469. // Set another completion function for the request above
  470. jqxhr.complete(function(){ alert("second complete"); });
  471.  
  472. //--------------------------------------------
  473.  
  474. <!DOCTYPE html>
  475. <html>
  476. <head>
  477.   <style>img{ height: 100px; float: left; }</style>
  478.   <script src="http://code.jquery.com/jquery-latest.js"></script>
  479. </head>
  480. <body>
  481.   <div id="images">
  482.  
  483. </div>
  484. <script>
  485. $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
  486.   {
  487.     tags: "mount rainier",
  488.     tagmode: "any",
  489.     format: "json"
  490.   },
  491.   function(data) {
  492.     $.each(data.items, function(i,item){
  493.       $("<img/>").attr("src", item.media.m).appendTo("#images");
  494.       if ( i == 3 ) return false;
  495.     });
  496.   });</script>
  497.  
  498. </body>
  499. </html>
  500.  
  501. //--------------------------------------------
  502.  
  503. $.getJSON("test.js", function(json) {
  504.    alert("JSON Data: " + json.users[3].name);
  505.  });
  506.  
  507.  
  508. //--------------------------------------------
  509.  
  510. $.getJSON("test.js", { name: "John", time: "2pm" }, function(json) {
  511.     alert("JSON Data: " + json.users[3].name);
  512.     });
  513.  
  514. //--------------------------------------------
  515.  
  516. $.ajax({
  517.   url: url,
  518.   dataType: "script",
  519.   success: success
  520. });
  521.  
  522. //--------------------------------------------
  523.  
  524. $.getScript("ajax/test.js", function(data, textStatus, jqxhr) {
  525.    console.log(data); //data returned
  526.    console.log(textStatus); //success
  527.    console.log(jqxhr.status); //200
  528.    console.log('Load was performed.');
  529. });
  530.  
  531. //--------------------------------------------
  532.  
  533. $.getScript("ajax/test.js")
  534. .done(function(script, textStatus) {
  535.   console.log( textStatus );
  536. })
  537. .fail(function(jqxhr, settings, exception) {
  538.   $( "div.log" ).text( "Triggered ajaxError handler." );
  539. });  
  540.  
  541.  
  542. //--------------------------------------------
  543.  
  544. $( "div.log" ).ajaxError(function(e, jqxhr, settings, exception) {
  545.   if (settings.dataType=='script') {
  546.     $(this).text( "Triggered ajaxError handler." );
  547.   }
  548. });
  549.  
  550.  
  551. //--------------------------------------------
  552.  
  553. $.ajaxSetup({
  554.   cache: true
  555. });
  556.  
  557. //--------------------------------------------
  558.  
  559. jQuery.cachedScript = function(url, options) {
  560.  
  561.   // allow user to set any option except for dataType, cache, and url
  562.   options = $.extend(options || {}, {
  563.     dataType: "script",
  564.     cache: true,
  565.     url: url
  566.   });
  567.  
  568.   // Use $.ajax() since it is more flexible than $.getScript
  569.   // Return the jqXHR object so we can chain callbacks
  570.   return jQuery.ajax(options);
  571. };
  572.  
  573. // Usage
  574. $.cachedScript("ajax/test.js").done(function(script, textStatus) {
  575.   console.log( textStatus );
  576. });
  577.  
  578. //--------------------------------------------
  579.  
  580. <!DOCTYPE html>
  581. <html>
  582. <head>
  583.   <style>
  584. .block {
  585.    background-color: blue;
  586.    width: 150px;
  587.    height: 70px;
  588.    margin: 10px;
  589. }</style>
  590.   <script src="http://code.jquery.com/jquery-latest.js"></script>
  591. </head>
  592. <body>
  593.  
  594. <button id="go">&raquo; Run</button>
  595.  
  596. <div class="block"></div>
  597.  
  598. <script>
  599. $.getScript("/scripts/jquery.color.js", function() {
  600.   $("#go").click(function(){
  601.     $(".block").animate( { backgroundColor: "pink" }, 1000)
  602.       .delay(500)
  603.       .animate( { backgroundColor: "blue" }, 1000);
  604.   });
  605. });
  606. </script>
  607.  
  608. </body>
  609. </html>
  610.  
  611. //--------------------------------------------
  612.  
  613. $('.log').ajaxComplete(function() {
  614.   $(this).text('Triggered ajaxComplete handler.');
  615. });
  616.  
  617. $('.trigger').click(function() {
  618.   $('.result').load('ajax/test.html');
  619. });
  620.  
  621.  
  622. //--------------------------------------------
  623.  
  624. $('.log').ajaxComplete(function(e, xhr, settings) {
  625.   if (settings.url == 'ajax/test.html') {
  626.     $(this).text('Triggered ajaxComplete handler. The result is ' +
  627.                      xhr.responseHTML);
  628.   }
  629. });
  630.  
  631.  
  632. //--------------------------------------------
  633.  
  634. $("div.log").ajaxError(function() {
  635.   $(this).text( "Triggered ajaxError handler." );
  636. });
  637.  
  638. $("button.trigger").click(function() {
  639.   $("div.result").load( "ajax/missing.html" );
  640. });
  641.  
  642. //--------------------------------------------
  643.  
  644. $( "div.log" ).ajaxError(function(e, jqxhr, settings, exception) {
  645.   if ( settings.url == "ajax/missing.html" ) {
  646.     $(this).text( "Triggered ajaxError handler." );
  647.   }
  648. });
  649.  
  650. //--------------------------------------------
  651.  
  652. $("#msg").ajaxError(function(event, request, settings){
  653.   $(this).append("<li>Error requesting page " + settings.url + "</li>");
  654. });
  655.  
  656. //--------------------------------------------
  657.  
  658. $.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
  659.   // Modify options, control originalOptions, store jqXHR, etc
  660. });
  661.  
  662.  
  663.  
  664. //--------------------------------------------
  665.  
  666. var currentRequests = {};
  667.  
  668. $.ajaxPrefilter(function( options, originalOptions, jqXHR ) {
  669.   if ( options.abortOnRetry ) {
  670.     if ( currentRequests[ options.url ] ) {
  671.       currentRequests[ options.url ].abort();
  672.     }
  673.     currentRequests[ options.url ] = jqXHR;
  674.   }
  675. });
  676.  
  677. //--------------------------------------------
  678.  
  679. $.ajaxPrefilter( function( options ) {
  680.   if ( options.crossDomain ) {
  681.     options.url = "http://mydomain.net/proxy/" + encodeURIComponent( options.url );
  682.     options.crossDomain = false;
  683.   }
  684. });
  685.  
  686. //--------------------------------------------
  687.  
  688. $.ajaxPrefilter( "json script", function( options, originalOptions, jqXHR ) {
  689.   // Modify options, control originalOptions, store jqXHR, etc
  690. });
  691.  
  692. //--------------------------------------------
  693.  
  694. $.ajaxPrefilter(function( options ) {
  695.   if ( isActuallyScript( options.url ) ) {
  696.     return "script";
  697.   }
  698. });
  699.  
  700. //--------------------------------------------
  701.  
  702. $('.log').ajaxSend(function() {
  703.   $(this).text('Triggered ajaxSend handler.');
  704. });
  705.  
  706. $('.trigger').click(function() {
  707.   $('.result').load('ajax/test.html');
  708. });
  709.  
  710. $('.log').ajaxSend(function(e, jqxhr, settings) {
  711.   if (settings.url == 'ajax/test.html') {
  712.     $(this).text('Triggered ajaxSend handler.');
  713.   }
  714. });
  715.  
  716. $("#msg").ajaxSend(function(evt, request, settings){
  717.         $(this).append("<li>Starting request at " + settings.url + "</li>");
  718.       });
  719.  
  720.  
  721. //--------------------------------------------
  722.  
  723. $('.log').ajaxStart(function() {
  724.   $(this).text('Triggered ajaxStart handler.');
  725. });
  726.  
  727. $('.trigger').click(function() {
  728.   $('.result').load('ajax/test.html');
  729. });
  730.  
  731. //--------------------------------------------
  732.  
  733. $("#loading").ajaxStart(function(){
  734.    $(this).show();
  735.  });
  736.  
  737. //--------------------------------------------
  738.  
  739. $('.log').ajaxStop(function() {
  740.   $(this).text('Triggered ajaxStop handler.');
  741. });
  742.  
  743. $('.trigger').click(function() {
  744.   $('.result').load('ajax/test.html');
  745. });
  746.  
  747. //--------------------------------------------
  748.  
  749. $('.log').ajaxSuccess(function() {
  750.   $(this).text('Triggered ajaxSuccess handler.');
  751. });
  752.  
  753. $('.trigger').click(function() {
  754.   $('.result').load('ajax/test.html');
  755. });
  756.  
  757. //--------------------------------------------
  758.  
  759. $('.log').ajaxSuccess(function(e, xhr, settings) {
  760.   if (settings.url == 'ajax/test.html') {
  761.     $(this).text('Triggered ajaxSuccess handler. The ajax response was:'
  762.                      + xhr.responseText );
  763.   }
  764. });
  765.  
  766. //--------------------------------------------
  767.  
  768. $("#msg").ajaxSuccess(function(evt, request, settings){
  769.       $(this).append("<li>Successful Request!</li>");
  770.       });
  771.  
  772. //--------------------------------------------
  773.  
  774.  
  775.  
  776. //--------------------------------------------
  777. //--------------------------------------------
  778.  
  779.  
  780. //--------------------------------------------
  781. //--------------------------------------------
  782. //--------------------------------------------
  783. //--------------------------------------------
  784.  
  785.  
  786. //--------------------------------------------
  787. //--------------------------------------------
  788. //--------------------------------------------
  789. //--------------------------------------------
  790. //--------------------------------------------
  791.  
  792.  
  793. //--------------------------------------------
  794. //--------------------------------------------
  795.  
  796. //----------------------------------------------------------------------------------------------
  797.   this.ajax_request=function (param, f3) {
  798.      
  799.       $.ajax ( {
  800.          type: "POST",
  801.          url: this.options.ajax_url,
  802.          data: param} ).done(
  803.             function(html) {f3.targetEl.ajax_result=f3.show(html);});
  804.            
  805.     }
  806. //----------------------------------------------------------------------------------------------
  807.   function createHTTP() {
  808.       var r;
  809.       try {
  810.          r = new XMLHttpRequest();
  811.       } catch(e) {
  812.          try {
  813.             r = new ActiveXObject('Msxml2.XMLHTTP');
  814.          } catch (e) {
  815.             r = new ActiveXObject('Microsoft.XMLHTTP');
  816.          }
  817.       }
  818.       return r;
  819.    }
  820.  
  821.  function ajax_request(param, f3) {
  822.    
  823.       XHR = createHTTP();
  824.       XHR.stat=0;
  825.       XHR.resp='';
  826.       XHR.f3=f3;
  827.      
  828.       XHR.onreadystatechange=function() {
  829.          if (this.readyState==4) {
  830.             this.resp=this.responseText;
  831.             this.stat=this.readyState;
  832.            
  833.             this.f3(this.resp);
  834.             G_Sending=false;
  835.            
  836.          }
  837.          else if (this.readyState==3) {
  838.             this.stat=this.readyState;
  839.          }               
  840.       }
  841.      
  842.       XHR.open('POST','req4.php',true);
  843.       XHR.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  844.       XHR.send(param);           
  845.    }
  846. //----------------------------------------------------------------------------------------------
  847.  
  848.  function ajax_request(param, f3) {
  849.    
  850.       if (gl_loading)
  851.          return;
  852.          
  853.       gl_loading=true;
  854.       showLoading(true);
  855.      
  856.       //----------------------------
  857.       function showLoading(on) {
  858.          if (on) {
  859.             $('#ajax_wait').css('width',screen.width+'px');
  860.             $('#ajax_wait').css('height',screen.height+'px');
  861.             $('#ajax_wait').css('display','block');
  862.             }
  863.          else {
  864.             $('#ajax_wait').css('display','none');
  865.          }
  866.       }
  867.       //----------------------------
  868.       function onSuccess(html) {
  869.          f3(html);
  870.          showLoading(false);
  871.          gl_loading=false;
  872.       }
  873.       //----------------------------
  874.       function onAjaxError(xhr,status) {
  875.          
  876.          showLoading(false);
  877.          gl_loading=false;
  878.          
  879.          var errinfo = {errcode: status};
  880.          if (xhr.status!=200) {
  881.             errinfo.message=xhr.statusText;
  882.             }
  883.          else {
  884.             errinfo.message='incorrect data';
  885.          }
  886.          onLoadError(errinfo);
  887.       }
  888.      
  889.       //----------------------------
  890.       function onLoadError(errinfo) {
  891.          var mag = 'error '+errinfo.errcode;
  892.          if (errinfo.message)
  893.             msg+=' :'+errinfo.message;
  894.            
  895.          alert(msg);
  896.       }
  897.      
  898.       //----------------------------
  899.       if (location.hostname=='localhost')
  900.          req_url='../learn/picport/req4.php';
  901.       else
  902.          if (location.hostname.indexOf('dmkim1.bget.ru')!=-1)
  903.             req_url='/learn/picport/req4.php';
  904.            
  905.          else
  906.             if (location.hostname.indexOf('ubezhishche.ru')!=-1)
  907.                req_url='reqajax.php';
  908.             else
  909.                req_url='../phpnew/picport/req4.php';
  910.          
  911.        
  912.       $.ajax( {
  913.          type: 'GET',
  914.          url: req_url,
  915.          data: param,
  916.          success: onSuccess,
  917.          error: onAjaxError,
  918.          cache: false
  919.          
  920.       });
  921.    }
  922. //----------------------------------------------------------------------------------------------
  923.  
  924. function createHTTP() {
  925.    var r;
  926.    try {r= new XMLHttpRequest() }
  927.    catch(e) {
  928.    try {r=new ActiveXObject('Msxml2.XMLHTTP');}
  929.    catch(e) {r=new ActiveXObject('Microsoft.XMLHTTP')}
  930.    }
  931.    
  932.    return r;
  933. }
  934. //--------------------------------------------------------
  935. function ajax_request(param,el) {
  936.  
  937.    XHR=createHTTP();
  938.    XHR.el=el;
  939.    XHR.onreadystatechange= function() {
  940.     //alert(this.readyState);
  941.       if (this.readyState==4) {
  942.        
  943.          //if (this.status=200) {
  944.             el3=document.getElementById(this.el);
  945.             el3.innerHTML=this.responseText
  946.             //}
  947.         G_Sending=false;   
  948.         }  
  949.       else  if (this.readyState==3) {
  950.          el3=document.getElementById(this.el);
  951.          el3.innerHTML='Waiting...';
  952.       }  
  953.    }
  954.    
  955.    XHR.open('POST','upload3.php',true);
  956.    XHR.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  957.    XHR.send(param);
  958.    
  959.    //XHR.open('GET','upload3.php?'+param,true);
  960.    //XHR.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  961.    //XHR.send(null);
  962.    
  963. }
  964.  
  965. //----------------------------------------------------------------------------------------------
  966.  
  967.  this.ajax_request = function (param, f3) {
  968.      
  969.          $.ajax ( {
  970.             type: 'POST',
  971.             url:  this.options.ajax_url,
  972.             data: param
  973.          }
  974.          ).done(
  975.             function(html) {
  976.                f3.statEl.ajax_result=f3.show(html);
  977.                my.wr('finished: '+f3.statEl.testb);
  978.                my.loading=false;
  979.             }
  980.          )
  981.       }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement