将ajax成功值传递给JS函数

时间:2015-04-27 06:42:10

标签: java javascript jquery ajax

我有一个从后端servlet获取值的ajax,现在我想将成功值传递给Java脚本函数。如何传递?

应在此行插入成功值 位置:[result3],

这是我的Java脚本:

<script type="text/javascript">


  var result3;
  function main()
  {
     $.ajax({
            url:'insertPos',
        //  data: {data : data},
            type:'get',             
             success:function(value)    
            {
                  result3= value;
                  callBackHnadler(result3)

            }

        });

  }

  function callBackHnadler(response) {



    MQA.EventUtil.observe(window, 'load', function() {

    /*Create an object for options*/
    var options={
      elt:document.getElementById('map'),        /*ID of element on the page where you want the map added*/
      zoom:12,                                    /*initial zoom level of map*/
      latLng:{lat: 12.922050, lng: 77.559933},   /*center of map in latitude/longitude*/
      mtype:'osm'                                /*map type (osm)*/
      };

    /*Construct an instance of MQA.TileMap with the options object*/
    window.map = new MQA.TileMap(options);

    MQA.withModule('route','routeio','largezoom','mousewheel','viewoptions',function() {



      /*Creates an MQA.RouteIO instance giving the endpoint of our new Directions Service as the
      first parameter and true that we will not use a proxy as the second (enables JSONP support to
      avoid server side proxies for route request and results communication).*/

      var io = new MQA.RouteIO(MQROUTEURL,true);

      /*Creates an MQA.RouteDelegate for defining default route appearance and behavior.*/
      delegate = new MQA.Route.RouteDelegate();

      /*The delegate is also required to customize the pois, in this sample adds rollover content*/

      var strContent = new String();
      strContent += "<h1><i><p><a href='javascript:insert();'>insert</a></p></i></h1>   <h1><i><p><a href='javascript:RePosition();'>re-position</a></p></i></h1>  <h1><i><p><a href='javascript:remove();'>remove</a></p></i></h1>    <h1><i><p><a href='javascript:convert();'>convert</a></p></i></h1> ";
        delegate.customizePoi = function(thePoi) {
        thePoi.setRolloverContent("Stop # : " + thePoi.stopNumber);
        thePoi.setInfoContentHTML(strContent);
        };  

      /*Creates an MQA.RouteController for managing the route while it is on the map. A key function of the route
      controller is to provide dragging capabilities to a route. The last parameter here is optional and is what is used
      to determine if the route should be draggable (this will be true by default if not provided).*/
    routeController= map.createRoute(delegate,io,{routeOptions:{routeType:'fastest'},ribbonOptions:{draggable:true, draggablepoi:true,ribbonDisplay:{color:"#00FF40", colorAlpha:0.75}}});


      /*Executes the route request back to our Directions Service.*/
      io.route({

        /*First parameter to the MQA.RouteIO.route function defines the route request.*/
        locations: [response], 
        mapState: routeController.delegate.virtualMapState(map)
       },

      /*The second parameter to the MQA.RouteIO.route function defines the IO settings.*/
      { timeout: 10000 },

      /*The final parameter to the MQA.RouteIO.route function is the callback function to execute
      the IO request as completed.*/
      function(results) {

        /*Takes the results of the route request and applies them to the map. The route ribbon will
        be drawn on the map along with the placement of POI's at each point.*/
        routeController.setRouteData(results.route);


        /*Change the map to display a best fit of the data.*/
        map.bestFit();


      });                                                                                                                                                        
         map.addControl(
         new MQA.LargeZoom(),
         new MQA.MapCornerPlacement(MQA.MapCorner.TOP_LEFT, new MQA.Size(1505,50)));   
         map.enableMouseWheelZoom();
         map.addControl(new MQA.ViewOptions());

     });     

  });

  } 
</script>

1 个答案:

答案 0 :(得分:0)

试试这个,

function callBackHnadler(response) {
  console.log(response);

}


var result3;

function main() {
  $.ajax({
    url: 'insertPos',
    //  data: {data : data},
    type: 'get',
    success: function(value) {
      result3 = value;
      callBackHnadler(result3)

    }

  });

}