Select to view content in your preferred language

NOAA.gov Crossdomain issue for custom Magnetic Declination Widget

1156
3
04-23-2014 09:46 AM
PatrickRhodes1
Occasional Contributor
Hello All,

I created a custom widget that works well developed from my pc but when I push it to the server it fails--.  I know there are potential proxy/php work arounds but I think it may be useful to others so if I could fix it and share it like the other widgets.  Here is the source code. 

<?xml version="1.0" encoding="utf-8"?>
<!--
///////////////////////////////////////////////////////////////////////////
// Copyright (c) 2010-2011 Esri. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

creationComplete="userRequest.send()"


<s:HTTPService id="userRequest" url="http://www.ngdc.noaa.gov/geomag-web/#declination"
useProxy="false" method="GET">
<mx:request xmlns="">
<lat1>{username.text}</lat1>
<lon1>{emailaddress.text}</lon1>
<resultFormat>xml</resultFormat>
</mx:request>        
</s:HTTPService>

userRequest.send();
<mx:DataGrid id="dgUserRequest" x="200" y="300" width="300"
dataProvider="{userRequest.lastResult.maggridresult.result}">
<mx:columns>
<mx:DataGridColumn headerText="Declination (Retain Negative)" dataField="declination"/>   
<mx:DataGridColumn headerText="Date" dataField="date"/>      
</mx:columns>
</mx:DataGrid>


///////////////////////////////////////////////////////////////////////////
-->



<viewer:BaseWidget xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:mx="library://ns.adobe.com/flex/mx"
       xmlns:esri="http://www.esri.com/2008/ags"
                   xmlns:viewer="com.esri.viewer.*"
                   widgetConfigLoaded="init()"
       creationComplete="userRequest.send()"
       >
    <fx:Script>
        <![CDATA[
   import com.esri.ags.events.MapMouseEvent;
   import com.esri.ags.geometry.MapPoint;
   import com.esri.ags.utils.WebMercatorUtil;
   import flash.net.*
  

   

            //this function called when the widget's configuration is loaded
  
            private function init():void
            {
                if (configXML) // checking for valid content in the configuration file
                {
                    lbl.text = configXML.content || getDefaultString("helloContent");
                }
            }
   private function helloWorld_openHandler (event:Event):void
   {
    map.addEventListener(MapMouseEvent.MAP_CLICK,mapClicked);
   } 

   private function mapClicked (event:MapMouseEvent) :void
   {
    var content:Object = {};
    content.wm = event.mapPoint;
    content.ll = WebMercatorUtil.webMercatorToGeographic(event.mapPoint) as MapPoint;   
    username.text= ""+content.ll.y;
    emailaddress.text=  ""+content.ll.x;
    userRequest.send();

   }
   protected function helloWorld_closedHandler(event:Event):void
   {
    map.removeEventListener(MapMouseEvent.MAP_CLICK,mapClicked);   
   }
   public function handleLink():void
   {
    var newLink:URLRequest = new URLRequest(url.text);
    navigateToURL(newLink,"_blank");
   }
  ]]>
    </fx:Script>
<fx:Declarations>
  <s:HTTPService id="userRequest" url="http://www.ngdc.noaa.gov/geomag-web/calculators/calculateDeclination.xml"
        useProxy="false" method="GET">
   <mx:request xmlns="">
    <lat1>{username.text}</lat1>
    <lon1>{emailaddress.text}</lon1>
    <resultFormat>xml</resultFormat>
   </mx:request>        
  </s:HTTPService>
</fx:Declarations>
    <viewer:WidgetTemplate id="helloWorld"
                           width="400" height="300" open="helloWorld_openHandler(event)" closed="helloWorld_closedHandler(event)">
        <viewer:layout>
            <s:VerticalLayout horizontalAlign="center" verticalAlign="middle"/>
        </viewer:layout>

        <s:Label id="lbl"
                 width="100%"
                 fontSize="18"
                 fontStyle="italic"
                 fontWeight="bold"/>
  <mx:Form x="200" y="200" width="400">
   <mx:FormItem>
    <s:Label text="Latitude" />    
    <s:TextInput id="username" />
   </mx:FormItem>
   <mx:FormItem>
    <s:Label text="Longitude" />    
    <s:TextInput id="emailaddress" />
    </mx:FormItem>
 
   </mx:Form>
  <mx:Text id="url" text="http://www.ngdc.noaa.gov/geomag-web/#declination"/>
  <mx:Button label="Open New Window" click="handleLink()"/>
  <mx:DataGrid id="dgUserRequest" x="200" y="300" width="300"
      dataProvider="{userRequest.lastResult.maggridresult.result}">
   <mx:columns>
    <mx:DataGridColumn headerText="Declination (Retain Negative)" dataField="declination"/>   
    <mx:DataGridColumn headerText="Date" dataField="date"/>      
   </mx:columns>
  </mx:DataGrid>
    </viewer:WidgetTemplate>
</viewer:BaseWidget>

The widget config.xml is based off HelloWorld widget
<configuration>

    <content>Magnetic Declination</content>

</configuration>
[ATTACH=CONFIG]33279[/ATTACH]

Error after compiling:

[RPC Fault faultString="HTTP request error" faultCode="Server.Error.Request" faultDetail="Error: [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032: Stream Error. URL: http://localhost:37813/geomag-web/calculators/calculateDeclination.xml?lon1=&resultFormat=xml&lat1=?..."]. URL: http://www.ngdc.noaa.gov/geomag-web/calculators/calculateDeclination.xml"]
at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::faultHandler()
at mx.rpc::Responder/fault()
at mx.rpc::AsyncRequest/fault()
at DirectHTTPMessageResponder/errorHandler()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()

Fiddler on the server just hangs I think it might be a security issue but it still works on my PC.
Tags (2)
0 Kudos
3 Replies
PatrickRhodes1
Occasional Contributor
It worked with this proxy found here put in an text file in my wwwroot folder.

<?php

$post_data = $HTTP_RAW_POST_DATA;

$header[] = "Content-type: text/xml";
$header[] = "Content-length: ".strlen($post_data);

$lat1 = $_GET['lat1'];
$lon1 = $_GET['lon1'];

$url = "http://www.ngdc.noaa.gov/geomag-web/calculators/calculateDeclination.xml?lat1=$lat1&lon1=$lon1&resul...";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

if ( strlen($post_data)>0 ){
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}

$response = curl_exec($ch);    
$response_headers = curl_getinfo($ch);    

if (curl_errno($ch)) {
    print curl_error($ch);
} else {
    curl_close($ch);
    header( 'Content-type: ' . $response_headers['content-type']);
    print $response;
}
?>

It was parsing the URL generated by HTTP request from flash.
I did not know what to do with the xml parameters, but it works ok I guess.
0 Kudos
PatrickRhodes1
Occasional Contributor
checked with the admin at noaa, and said that they are not yet ready to add a cross domain for the subdomain that houses the magnetic data, so php is the way to go.
0 Kudos
BjornSvensson
Esri Regular Contributor
FYI - for people looking for a more full-fletched PHP proxy, or similar proxies for DotNet and JSP, see: https://github.com/Esri/resource-proxy.  (as mentioned on the Flex Viewer resource center - http://resources.arcgis.com/en/help/flex-viewer/concepts/#/Using_a_proxy_page/01m30000000w000000/)
0 Kudos