not extracting values correctly from URL

1044
18
08-30-2012 08:47 AM
deleted-user-ugCMpXci8bn5
New Contributor III
Hello,

I am trying to create a URL from the current extent (and including other parameters), and then be able to extract that extent from the URL and zoom to that extent on opening the app, with a default extent if there are no parameters in the URL.

What I am getting instead is an empty map when I load a URL with parameters.  I am not getting any errors in Firebug.  Below is the relevant code:

(to extract parameters:)
    
    var urlToObject = locale.substring(locale.indexOf("?") + 1, locale.length);    
    var testURLExtent;    
    urlToObject = dojo.queryToObject(urlToObject);
  
  if ( urlToObject.urlxmin != null) 

  { 
   alert ("passed in extent from url");
  
   var locXmin = urlToObject.urlxmin;
   var locYmin = urlToObject.urlymin;
   var locXmax = urlToObject.urlxmax;
   var locYMax = urlToObject.urlymax;

  }
  
  else
  {
   alert ("no params, preset extent");
   var locXmin = 896338.273119375;
   var locYmin = 116730.668986593;
   var locXmax = 1089315.70367493;
   var locYMax = 274621.293986593;
  }
  
  var testURLExtent = new esri.geometry.Extent (locXmin, locYmin, locXmax, locYMax,  new esri.SpatialReference({ wkid: 2263}));


(and to create URL, from a new extent:)
    var curExtent = {
   urlxmin: pointExtent.xmin,
   urlymin: pointExtent.ymin,
   urlxmax: pointExtent.xmax,
   urlymax: pointExtent.ymax,
   
    };
    
          var url = "http://127.0.0.1:8020/MyApp/index.html";
    var queryStr = dojo.objectToQuery(curExtent);
    url= url + "?" + queryStr;
    alert (" url: " + url);



I notice that if I use only one parameter (such as xmin), and set the rest to constant values. the value is passed correctly from the URL, making me think there is a problem with the parsing of the parameters at the '&' sign?
Any help would be appreciated, thanks //
0 Kudos
18 Replies
KellyHutchins
Esri Frequent Contributor
Try using the esri.urlToObject to convert the url params to an object. Here's a sample that shows how this works:

http://help.arcgis.com/en/webapi/javascript/arcgis/help/jssamples/exp_history.html
0 Kudos
deleted-user-ugCMpXci8bn5
New Contributor III
Thanks, I did see that example and tried using the esri function, but without success... Is there an esri objectToUrl function?
0 Kudos
deleted-user-ugCMpXci8bn5
New Contributor III
I think my problem is that with either the dojo or esri function, I do not understand how to extract multiple parameters.  Also, I am using v2.1 of the JS API, if that is relevant.  Thanks again /
0 Kudos
KellyHutchins
Esri Frequent Contributor
Here's a snippet that will extract the current extent from the url and zoom to that

        var urlObj = esri.urlToObject(document.location.href);
        if(urlObj.query && urlObj.query.extent){
            map.setExtent(new esri.geometry.Extent(dojo.fromJson(urlObj.query.extent)));
        }


Here's a sample url that includes the extent.
http://localhost/test.html?extent={%22xmin%22:-13680584.814157216,%22ymin%22:5950709.454900841,%22xmax%22:-13678688.22039284,%22ymax%22:5952207.142923844,%22spatialReference%22:{%22wkid%22:102100}}

And here's an example of creating the url params to append to the url

  extent = "?extent=" + dojo.toJson(map.extent.toJson());
0 Kudos
deleted-user-ugCMpXci8bn5
New Contributor III
When I tried this, the URL is created as expected (output to console), but when I cut and paste the URL and try to navigate to it, I get "NetworkError: 500 Internal Server Error" followed by the URL , in Firebug. Ideas?
0 Kudos
KellyHutchins
Esri Frequent Contributor
Sounds like the url you are trying to navigate to is invalid. Can you post it here so we can take a look?
When I tried this, the URL is created as expected (output to console), but when I cut and paste the URL and try to navigate to it, I get "NetworkError: 500 Internal Server Error" followed by the URL , in Firebug. Ideas?
0 Kudos
deleted-user-ugCMpXci8bn5
New Contributor III
here it is:

http://127.0.0.1:8020/SchoolSafety/index.html?urlextent={"xmin":981583.5277170027,"ymin":195611.84174998308,"xmax":981583.5301690912,"ymax":195611.84374998306,"spatialReference":{"wkid":2263}}

thanks, Jason
0 Kudos
KellyHutchins
Esri Frequent Contributor
Jason,

Use encodeURIComponent to encode the URL parameter. Here's some more info:

http://stackoverflow.com/questions/75980/best-practice-escape-or-encodeuri-encodeuricomponent
0 Kudos
deleted-user-ugCMpXci8bn5
New Contributor III
I tried following the examples on that page:

        var url = "http://127.0.0.1:8020/SchoolSafety/index.html";
    urlParam = encodeURIComponent(dojo.toJson(pointExtent.toJson()));
    url = url + "/?urlextent=" + urlParam;

and


        var url = "http://127.0.0.1:8020/SchoolSafety/index.html";
    urlextent = "/?urlextent=" + dojo.toJson(pointExtent.toJson());
    url = url + urlextent;
    url = encodeURI(url);

..and some other variations, but all resulted in the same 'Internal Service Error'.  What am I missing?
0 Kudos