Alpha DevCon 2018
Results 1 to 8 of 8

Thread: passing JSON from Xbasic function to javascript

  1. #1
    Member
    Real Name
    Josh Moshier
    Join Date
    May 2013
    Posts
    322

    Default passing JSON from Xbasic function to javascript

    Below is code that pulls data off my dialog (lat and lngs) and uses them to build the starting, stopping, and waypoints to be used in a google maps api call (right now using jQuery's getJSON but there is issues using this so wanting to do the call using xBasic).

    Code:
            var allLat = {dialog.Object}.harvestRepeatingSectionColumn('lat','\n');
    	var allLng = {dialog.Object}.harvestRepeatingSectionColumn('lng','\n');
    	
    	var aLat = allLat.split('\n');
    	var aLng = allLng.split('\n');
    	
    	var txt = [];
    
    	for(var i = 1; i <= aLat.length ; i++) {
    		txt.push(aLat[i-1] + ',' + aLng[i-1]);
    	}
    	
    	var last = txt.length - 1;
    		
    	var waypoints = [];
    	
    	
    		for(var i = 1; i<last; i++){
    		waypoints.push(txt[i]);
    		
    		}
    	
    		
    		waypoints = waypoints.join('|')
    
    
                   var $jq = jQuery.noConflict();
    		
    		 $jq.getJSON('http://maps.googleapis.com/maps/api/directions/json?origin=' + txt[0] + '&destination=' + txt[last] + '&waypoints=' + waypoints + '&sensor=false',function(results){
    I want implement this part of my code in an xBasic function that will be called using an ajax callback in action javascript on a button allowing me to get the JSON and then I want to pass this JSON back to a javascript function to do the parsing (as I have about 5000 lines of code here and don't want to try to convert this to xBasic). Can someone just get me started on how I will harvest data from the repeating section and then build the necessary array (including joining the waypoints array with this character '|' as this is necessary for the goggle api call).

    Thanks!

    Jmo

  2. #2
    Member
    Real Name
    Josh Moshier
    Join Date
    May 2013
    Posts
    322

    Default Re: passing JSON from xBasic function to javascript

    As far as I can tell this is not feasible as the xBasic function can't read controls from browser, is this correct? If this is the case is there a way to take that url I've created, put it in a variable, and then do an ajax callback of an xBasic function passing in that url as a variable?

  3. #3
    Moderator
    Real Name
    Steve Workings
    Join Date
    Apr 2000
    Location
    The Dreaded Chair
    Posts
    5,510

    Default Re: passing JSON from xBasic function to javascript

    Josh, I've read this a few times, not sure what you need.

    Have you considered:

    {Dialog.Object}.ajaxCallback('','',xbasicFunction,'' [,additionalData [,optionsObject]])

    and

    See video # M58 re: the state object and variables
    -Steve


  4. #4
    Member
    Real Name
    Josh Moshier
    Join Date
    May 2013
    Posts
    322

    Default Re: passing JSON from xBasic function to javascript

    I am unable to get on my server right now (someone else is developing and I haven't yet created a virtual environment on my Mac to develop locally), but I do believe that is what I am looking for. Just to be sure, with that call I am able to call the xBasic function I want to run and can pass in my url as a character variable in the additional data part? Thanks!

  5. #5
    Moderator
    Real Name
    Steve Workings
    Join Date
    Apr 2000
    Location
    The Dreaded Chair
    Posts
    5,510

    Default Re: passing JSON from xBasic function to javascript

    Yeah, it'll take anything. From the example:

    Code:
    {dialog.Object}.submit();
    //Include an optional submitType value and additional data
    {dialog.Object}.submit('type1','var1=alpha&var2=beta');
    So you have name/value pairs. If your name is "var1" then your Xbasic function will have an "e.var1" variable to work with.
    -Steve


  6. #6
    Member
    Real Name
    Josh Moshier
    Join Date
    May 2013
    Posts
    322

    Default Re: passing JSON from xBasic function to javascript

    I have the URL passing into the xbasic function which I am calling using the Ajax callback action JavaScript (url is being built and returned with a javascript function). Below is my code and the way it works is a button calls getJson(); and the 'ajaxJson' action gets the returned value from urlBuild().

    Code:
    function getJson () {
    
    {dialog.object}.runAction('ajaxJson');
    
    }
    
    function urlBuild () {
    
         return "varurl=http://maps.googleapis.com/maps/api/directions/json?origin=Huntsville,AL&destination=Atlanta,GA&sensor=false";
    }
    Code:
    function getdata as c (e as p)
    
    dim url as c
    dim txt as c
    dim p as p
    url = e.varurl + "&destination=" + e.destination + "&sensor=" + e.sensor
    txt = http_get_page2(url)
    p = json_parse(txt)
    
    dim distance as c
    distance = p.routes[1].legs[1].distance.value
    
    dim js as c
    js = "$('msg1').innerHTML='" + distance + "';"
    
    getdata = js
    
    end function

    If I post "url" to the msg1 div it shows up exactly how it should, however when I post distance it tells me that p.routes[] subelement doesn't exist (however I know it does)? I also tried to post "txt" to msg1 and I get a "string constant not terminated" error? To test this without using the javascript or the building of the url variable I used the code below (with a different button that just did the ajax callback)

    Code:
    function xb as c (e as p)
    
    dim url as c
    dim txt as c
    dim p as p
    url = "http://maps.googleapis.com/maps/api/directions/json?origin=Huntsville,AL&destination=Atlanta,GA&sensor=false"
    txt = http_get_page2(url)
    p = json_parse(txt)
    
    dim distance as c
    distance = p.routes[1].legs[1].distance.value
    
    dim js as c
    js = "$('msg1').innerHTML='" + distance + "';"
    
    getdata = js
    
    end function
    I get the same errors as above when using this code, does anyone see anything wrong with either the original code or the testing code that would cause for these errors? As a note I am doing this through remote access to the server (not on my local machine) but I don't think this would make a difference. Thanks for the help!
    Last edited by lilmofo812; 11-12-2013 at 11:38 PM. Reason: In red is what I was missing in my code (forgot the 's' on directions) working now!

  7. #7
    Moderator
    Real Name
    Steve Workings
    Join Date
    Apr 2000
    Location
    The Dreaded Chair
    Posts
    5,510

    Default Re: passing JSON from xBasic function to javascript

    Again, I'm not sure what your question is, or exactly what problem you're having. I used the last example in your post, and it worked well but with one exception.

    If you change

    Code:
    getdata = js
    to

    Code:
    xb = js
    The static text object that I gave a div of "msg1" filled with the distance.
    -Steve


  8. #8
    "Certified" Alphaholic
    Real Name
    Larry Grupido
    Join Date
    May 2010
    Posts
    1,554

    Default Re: passing JSON from xBasic function to javascript

    Quote Originally Posted by Steve Workings View Post
    Yeah, it'll take anything. From the example:

    Code:
    {dialog.Object}.submit();
    //Include an optional submitType value and additional data
    {dialog.Object}.submit('type1','var1=alpha&var2=beta');
    So you have name/value pairs. If your name is "var1" then your Xbasic function will have an "e.var1" variable to work with.
    Actually, you will have

    e.rv.var1

Similar Threads

  1. a5_report_excel_save - or passing arguments to json?
    By marvinbase in forum Windows Desktop Applications
    Replies: 1
    Last Post: 02-20-2014, 10:29 AM
  2. Passing variables from row in list control to xbasic function
    By coleresources in forum Mobile & Browser Applications
    Replies: 4
    Last Post: 11-12-2013, 07:13 PM
  3. Call Javascript Function from Xbasic
    By canc in forum Application Server Version 11 - Web/Browser Applications
    Replies: 2
    Last Post: 02-28-2013, 12:48 PM
  4. Passing a parent grid Variable to an xbasic function.
    By Purvi in forum Application Server Version 10 - Web/Browser Applications
    Replies: 1
    Last Post: 03-20-2012, 03:00 PM
  5. ONCLICK passing parent recnum from xbasic to javascript
    By kimhere in forum Application Server Version 10 - Web/Browser Applications
    Replies: 0
    Last Post: 04-29-2011, 03:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •