AJAX on Netburner

Discussion to talk about software related topics only.
Post Reply
neil
Posts: 6
Joined: Fri Apr 05, 2013 9:06 am

AJAX on Netburner

Post by neil »

I have been following the example of Syncor's Using AJAX to Serve Dynamic Web Content on the NetBurner Platform.

In Example 2. Netburner method that responds to AJAX request

void AjaxCallback(int sock, const char* url)

Where is this example suppose to be placed in the main.c? In the while(1) loop, under void UserMain(void * pd)?

I am not sure, because placing this func, in either location generates an error.

If anyone is familiar with the example, please explain.

Thank you,
Neil P.
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: AJAX on Netburner

Post by tod »

First, I'm about to upload Version 3 so if you're just getting started you may want to wait. However, while I don't have the code for V2 open it should be similar to V3. In the Framework folder there is a file called HtmlCallbacks.cpp. That file already has the AjaxCallback method. While you can go modify that method that wasn't my intent. That method calls HtmlServices::ProcessAjaxFunction(). You shouldn't even modify that one. Instead my intent was that you would modify or add a pair to the AjaxCommands map that is at the top of HtmlServices.cpp. For new functionality you have to create the function that is part of that pair. Then when that ajax request comes in the proper function gets invoked and you don't have to worry about any of the Ajax plumbing other than the name of the request.
Here's how you would add a handler for a new ajax request for "resultData"

Code: Select all

AjaxCommands.insert(make_pair(_requestToken + "=resultData", &WriteSampleResultData));
Now you have to go write the function WriteSampleResultData() inside HtmlServices.

Code: Select all

void HtmlServices::WriteSampleResultData(int sock)
{
   //Get the results you want to write back to the netburner. 
   string results = YourCodeThatGeneratesData();
    writestring(sock, results.c_str());

}
neil
Posts: 6
Joined: Fri Apr 05, 2013 9:06 am

Re: AJAX on Netburner

Post by neil »

Sorry Tod. I have been following your other work and it seems you have improve on the bone structure of the white paper, although
I have no clue of what is going on, so I would like to stick for now with your bare bone structure way. I have been studying it for a while
and it seems the only missing piece of the puzzle was where to place the code for Example 2.

I have been watching turtorials on AJAX on line, but of course they don't use a Netburner (but has familiarties). I also have watched your video with the
syncor (syn.XXX), but that video also refers to V2 and it just flew right over me. I guess I'm trying to crawl before I can walk, if you know what I mean.

If I am correct Example2 is the only piece of code that goes in the main.cpp? Example 1 goes in a file created as ajax.htm, Examples 3,4,5,6 and 7 goes in
the file SupportScripts.js and finally the HTML.

Thanks.
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: AJAX on Netburner

Post by tod »

Sorry, my eyes saw "Example 2" and my brain translated that to "Version 2". You're really taxing my memory here. Let me make a guess at what's giving you a problem. The AjaxCallback method has to use "C" linkage, not "C++" linkage. This has nothing to do with Ajax, it's just how the callbacks are implemented by NB. The same is true of UserMain by the way. So there are a couple of ways to enforce that. I would suggest you create a file (I call mine HtmlCallbacks.cpp). Then I write all my HTML callbacks in that file after any using statements at the top I have an extern C statement as shown below.

Code: Select all

extern "C"
{
//Other functions used in web pages can go inside here as well
	//==============================================================================
	// Example function that can be called via AJAX. This one handles multiple
	// request..
	//==============================================================================
	void AjaxCallback(const int sock, const char* const url)
	{
		HtmlServices::ProcessAjaxFunction(sock, url);
		return;
	}
}
I also just went back to the white paper and the code that came with it. In webformcode.cpp you will find

Code: Select all

//==============================================================================
// Simple sample code to show how to implement a function callback from
// webpage
//==============================================================================
extern "C"
{

  //==============================================================================
  // Example function that can be called via AJAX. This one just writes out the
  // the current timetick value but of course this could be much more elaborate.
  //==============================================================================
  void AjaxCallback(int sock, const char* url)
  {
    char java_script[100];
    sprintf(java_script, " System Tick count:%u", TimeTick);
    writestring(sock, java_script );
  }
}
I think it's a good idea to do Ajax coding by hand ONCE to learn what's going on. Then you want to replace your raw XmlHttpRequest with one from a library like jQuery. They take care of all the incompatible browser issues. Also be aware, that as I remember, the white paper didn't deal with the fact that IE5-IE8 (and maybe IE9 and IE10) end up doing overly aggressive caching so only the first AjAX call gets real data, then IE just keeps serving up the previous results from cache. This topic is discussed multiple times on the forum. Again the libraries now have a property for turning off cache, with the raw request its a little more work.
Last edited by tod on Sat Apr 27, 2013 12:32 pm, edited 1 time in total.
User avatar
tony
Posts: 49
Joined: Thu Apr 24, 2008 6:05 pm

Re: AJAX on Netburner

Post by tony »

There is a complete example for the MOD54415 that has a built in IDE for the AngelScript Scripting language. The IDE has a few examples of using AJAX for browsing the SD Card as well as reporting script errors during compilation. The IDE was built with JQuery/UI and CodeMirror. Links to all of the info can be found here:

http://forum.embeddedethernet.com/viewt ... f=5&t=1448
http://wiki.embeddedethernet.com/AngelScript
Post Reply