0

I have a ajax which gets value from back-end servlet ,now I want to pass the success value into a Java script function.How to pass that??

The success value should be inserted at this line locations: [result3],

Here is my Java Script:

<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>
Ramesh K
  • 41
  • 6
  • tl;dr: You can't - instead, call `MQA.EventUtil.observe` from within the `success` function. – Amadan Apr 27 '15 at 06:45
  • I haven't tried that – Ramesh K Apr 27 '15 at 06:47
  • Easy: `success: function(value) { MQA.EventUtil.observe(....... locations: [value] ...... ); }` – Amadan Apr 27 '15 at 06:53
  • It is not working ,My map is not getting loadded – Ramesh K Apr 27 '15 at 06:58
  • Sorry, I don't know anything about maps, nor do I know what `MQA` is. But this is the only way to make use of information received from AJAX. You may wish to edit your question and append your most current code. Also be sure to take a look at the JavaScript console and note any errors that may have popped up and broken your code. – Amadan Apr 27 '15 at 07:00

1 Answers1

0

try this,

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)

    }

  });

}
syms
  • 413
  • 2
  • 12
  • I tried this one but not working – Ramesh K Apr 27 '15 at 06:52
  • Is there any error on console??? – syms Apr 27 '15 at 06:56
  • No error but my map is not getting loaded – Ramesh K Apr 27 '15 at 06:58
  • How make that 'result3' as global and pass it to location,If I print that result3 outside AJAX it is showing UNDEFINED – Ramesh K Apr 27 '15 at 07:00
  • Ok can you try to put everything from "MQA.EventUtil.observe(window, 'load', function() {" to "callBackHnadler" function – syms Apr 27 '15 at 07:01
  • @RameshK: Again, what you want is impossible, because of timing problems. You can't use a global `result3` in code that has not been appropriately delayed by invoking it from the `success` callback. The linked question has an exhaustive and educational answer by Felix Kling; go read it. – Amadan Apr 27 '15 at 07:01
  • actually your result3 is global but when you are calling it outside ajax your resquest may be still pending and waiting for response before that your code outside ajax getting called – syms Apr 27 '15 at 07:02
  • Ok..please check the edited code – Ramesh K Apr 27 '15 at 07:07
  • Remove "MQA.EventUtil.observe(window, 'load', function() {" this, I don't think you have to add event observer and try. – syms Apr 27 '15 at 07:21