AnsweredAssumed Answered

How do I use esriRequest in a widget?

Question asked by mathiaskopo on Nov 9, 2015
Latest reply on Nov 10, 2015 by mathiaskopo

Hello, can anyone guide me in a direction on how to use the esriRequest function in a Web Appbuilder Widget?

 

I have just started using Web AppBuilder Developer Edition. I started with extending the sample widget Demo with a button

Widget.html:

<div>
<button id="gotoPosition" data-dojo-type="dijit/form/Button" type="button" data-dojo-attach-point="gotoPosition">Go to position</button>
</div>

I then extended the onOpen: function() in widget.js to zoom to a position

  ...
 onOpen: function(){
  console.log('onOpen');
  var map = this.map;
  var Long, Lat;
  var LocPoint, point;   
  //Go to position
  on(this.gotoPosition, 'click', lang.hitch(this, function(evt){   
    console.log('gotoPosition 2');
    map.graphics.clear();
    Lat = 55;
    Long = 15;
    LocPoint = new Point([Long,Lat]);
    map.centerAndZoom(LocPoint,17);
    }));
  },
...

 

From Access data (XML) | ArcGIS API for JavaScript I found out how to use esriRequest and from Using the proxy | Guide | ArcGIS API for JavaScript how to install the proxy and finally putting it all together in one HTML file (esriRequest_xml.html) which works fine.

<!DOCTYPE html>
<html>
<head>
  <title>XML Content</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <link rel="stylesheet" href="http://js.arcgis.com/3.14/esri/css/esri.css">
  
  <script src="http://js.arcgis.com/3.13/"></script>
  <script>
    require(["dojo/dom", "dojo/on", "dojo/dom-class", "dojo/_base/json", "dojox/xml/parser", "esri/config", "esri/request", "dojo/domReady!"], function(dom, on, domClass, dojoJson, xmlParser, esriConfig, esriRequest) {
      console.log('ok')
      esriConfig.defaults.io.proxyUrl = "/PHP_proxy/proxy.php";
      var url = "url to a xml docement";
      var requestHandle = esriRequest({
        "url": url,
        "handleAs": "xml"
      });
      requestHandle.then(requestSucceeded, requestFailed);
      function requestSucceeded(response, io){
        document.write("Succes");
        //do something with the xml data
      }
      function requestFailed(error, io){
        document.write("Fail");
      }
    });
  </script>
</head>
<body>
  </body>
</html>

 

I then tried to put the javascript code from esriRequest_xml.html into the onOpen: function()  but this resulted in the error "Uncaught TypeError: Cannot read property 'io' of undefined" Maybe because of wrong scope?, but don't know how to fix this. Can anyone guide me how to get esriRequest to work?

Attachments

Outcomes