return data from a web POST request

Discussion to talk about software related topics only.
Post Reply
Lachlanp
Posts: 91
Joined: Tue Jan 29, 2013 4:44 am

return data from a web POST request

Post by Lachlanp »

I want to return data and indicate that a POST request was successful, but I am not sure how this is done with Netburner software

In the web page I have the following ajax call:

Code: Select all

  	$.ajax({type: "POST", url: 'TIMESET', async: false, data: t, timeout: 4000,
    		success: function(data){
			alert("OK" + data);
  			},
  		error: function () {
  			alert("Error updating time");
  			}
		});
In the Netburner I have redirected the POST function using

Code: Select all

oldPostHandler = SetNewPostHandler( doPost );		// Redirect Web page POST handler
and then the code to respond to the POST is as follows:

Code: Select all

int doPost( int sock, char *fname, char *pData, char *rxBuffer ){
	time_t RawTime;

	if (strcmp("/TIMESET",fname) == 0){
		RawTime = atol(pData);											// time from browser in seconds
		set_time( RawTime );											// Update the time
		sprintf(rxBuffer, "HTTP/1.1 200 OK\r\n[data]");
		return 0;
	}
	else 
	{
		sprintf(rxBuffer, "HTTP/1.1 400");
		return 1;
	}
}
This code sets the time OK but always response with 'error'.

How can I get the code to send back either OK or error, and can i send back data as well.
I don't think i have to put the HTTP code in the rxBuffer.
I would have thought that the return code would be the correct signal but I have tried 0 and 1 as return codes and both give 'error'.

any help would be appreciated.

Thanks
Lachlan
User avatar
pbreed
Posts: 1080
Joined: Thu Apr 24, 2008 3:58 pm

Re: return data from a web POST request

Post by pbreed »

You have to return the mime type etc....

#include <http.h> //For send header
#include <fdprintf.h> //For printf out a socket or file descriptor...

SendHTMLHeader( sock ); //Send the ok header and html mime type etc....

//Now send the actual page....
fdprintf(sock,"<HTML> <BODY> %s </BODY> </HTML>",data);


Creating a significant html response this way is hard...
I usually make my html page in the usual way to display the data, then redirect the post to
it...
Using...
void RedirectResponse( int sock, PCSTR new_page );

found in http.h...
Like this:

//This sends everything needed, no need to send any header before this response.
RedirectResponse( sock, "RESPONSE.HTML" );
Lachlanp
Posts: 91
Joined: Tue Jan 29, 2013 4:44 am

Re: return data from a web POST request

Post by Lachlanp »

thanks. with your help I have managed to fix the problem.
I couldnt get fdprint to work but once you pointed me in the correct direction, i managed to find other code.

I tried: fdprintf(sock, "<p>%s</p>", data);

What worked for me is simply:

SendHTMLHeader( sock ); //Send the ok header and html mime type etc....
writestring(sock, buffer);

Ever so simple when you know how.

Thanks greatly.
Lachlan
Post Reply