Topics Topics

jQuery Code Snippet

[source available at https://github.com/emakred/stuff/blob/master/jQuery_access_to_ARMS.md]

Sample jQuery Code

The following example is for a JSON-formatted response written with jQuery v1.9.1. Note:  ERS APIs require a key which can be obtained at http://api.data.gov.

<html>
    <title>USDA ERS API web service call using jSON</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
 
          /* CODE SAMPLE: retrieve list of ARMS reports using ERS API */**_
          function GetARMS() {      
            jQuery.support.cors = true;
 
            var jsonp_url = "http://api.data.gov/USDA/ERS/data/Arms/Reports?api_key=API_KEY&survey=Crop&callback=?";
 
            $.ajax({
              url: jsonp_url,                   
              type: 'GET',          
              dataType: 'jsonp',
              jsonpCallback: 'jsonCallback',               
              crossDomain: true,  
              success: function() { console.log("success"); },
              error: function() { console.log("error"); }                 
            });
          }
 
          function jsonCallback(data) {
               //alert('in jsonCallback');
            var reports = data.dataTable; // this array has the data
            alert(reports);     
            var strResult = "<table><th>Report Number</th><th>Report Description</th>";
 
            $.each(reports, function (index, Report) {
              strResult += "<tr><td>" + Report.report_num + "</td><td> " + Report.report_header + "</td></tr>";
            });
            strResult += "</table>"; 
            alert(strResult);    
 
            $("#divResultARMS").html(strResult);        
          }
 
        </script>
 
      <body>
        <h3>USDA ERS API demo </h3>
        <button onClick="GetARMS();return false;">ARMS Surveys</button></p>
        <div id="divResultARMS"></div>
      </body>
  </html>
 

Last updated: Monday, December 23, 2013

For more information contact: Website Administrator

Text Only Options

Top of page


Text Only Options

Open the original version of this page.

Usablenet Assistive is a UsableNet product. Usablenet Assistive Main Page.