"POST" handler executes twice from one HTML POST call

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

"POST" handler executes twice from one HTML POST call

Post by Lachlanp »

I have the following as the only javascript in my INDEX.HTM page

<script type="text/javascript">
$(document).ready(init);
function init(){ $.ajax({type: "POST", url: "test/config.jsn", async: false, data: "TEST" }); }
</script>

On my Netburner I have the code:
SetNewPostHandler( doPost );

When I access the index.htm page, it calls the function 'doPost' twice.

Is there any reason for this?
How do I stop it?

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

Re: "POST" handler executes twice from one HTML POST call

Post by tod »

do you also have a <body onload=""> attribute?

If you want a surefire way to see if you're accidentally calling it twice just put a

Code: Select all

debugger; 
statement inside init(); Then open a new tab on your browser, open the debug tools (Ctrl-Sh-I on Chrome) and then load your page.

BTW, you could also put your code in an anonymous function would make it impossible to invoke from somewhere else by accident.

Code: Select all

$(function() {
   $.ajax({type: "POST", url: "test/config.jsn", async: false, data: "TEST"
});
Note:

Code: Select all

$(document).ready(function(){}); 
is the equivalent of

Code: Select all

$(function(){});
Lachlanp
Posts: 91
Joined: Tue Jan 29, 2013 4:44 am

Re: "POST" handler executes twice from one HTML POST call

Post by Lachlanp »

My complete 100% index.htm page is:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jq_182.js"></script>
<script type="text/javascript">
$(document).ready(init);
function init(){
$.ajax({type: "POST", url: "test/config.jsn", async: false, data: "Testing" });
$("#ab").html("Done");
}
</script>
</head>
<body>
<div id = "ab"></div>
</body>
</html>

In my Netburner code I have the statement:
SetNewPostHandler( doPost );

In my function:
int doPost( int sock, char *fname, char *pData, char *rxBuffer )

I have a break point set. When I open the web page, the break point occurs twice before "Done" appears on the Web client.

Any ideas why this may occur?

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

Re: "POST" handler executes twice from one HTML POST call

Post by tod »

I would guess doPost is doing something in response to your synchronous ajax call that causes the page to reload. It's always easier to help if there's code.
Lachlanp
Posts: 91
Joined: Tue Jan 29, 2013 4:44 am

Re: "POST" handler executes twice from one HTML POST call

Post by Lachlanp »

Attached is my complete 100% Netburner code. I have already posted my complete 100% html page.
Note that the increment x++ occurs twice before the Web page shows "Done".

Further testing shows that this only occurs with Firefox. It does not occur with Internet Explorer and Google Chrome. I use Firefox because it is easy to see the code in the web page (with Firebug) and it displays the information exactly as an iPad does (the eventual user).

#include "predef.h"
#include <stdio.h>
#include <ctype.h>
#include <startnet.h>
#include <autoupdate.h>
#include <dhcpclient.h>
#include <NetworkDebug.h>
#include "http_f.h"
const char *AppName = "WEB POST Test";
extern "C" { void UserMain(void * pd); }
int doPost( int sock, char *url, char *pData, char *rxBuffer );
char x = 0;

void UserMain( void *pd ){
InitializeStack();
OSChangePrio( MAIN_PRIO );
EnableAutoUpdate();
StartHTTP();
if (EthernetIP == 0) GetDHCPAddress();
#ifdef _DEBUG
InitializeNetworkGDB_and_Wait();
#endif
SetNewPostHandler( doPost );
while ( 1 ){
OSTimeDly( TICKS_PER_SECOND );
}
}

int doPost( int sock, char *fname, char *pData, char *rxBuffer ){
x++;
return 0;
}

Any ideas?

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

Re: "POST" handler executes twice from one HTML POST call

Post by tod »

Maybe you should try a more recent version of jquery? I had 1.9.1 lying about (the most recent is 1.10.x) and I ran your HTML code (with an alert() inserted into init() ) from Chrome on a PC, Firefox on a Mac (the only place I have it installed) and from Safari on an iPad. None of them showed the alert more than once. Could be that version of jquery had an event bubbling error on FF.

BTW, as far as I can tell the debugger tools in Chrome do everything you can do in Firebug. You can view html code from the Resources, Sources and Elements tabs. There is also a little magnifying glass inspector tool at the bottom when you want to just click on part of page and see the associated html (which was a feature I always liked in Firebug).
rnixon
Posts: 833
Joined: Thu Apr 24, 2008 3:59 pm

Re: "POST" handler executes twice from one HTML POST call

Post by rnixon »

How about wireshark to see if it gets called twice?
ecasey
Posts: 164
Joined: Sat Mar 26, 2011 9:34 pm

Re: "POST" handler executes twice from one HTML POST call

Post by ecasey »

Lachlan,

I ran your code on my MOD5441X. I used jquery-1.8.3.js because I couldn't find 182.
It worked fine. Only hit doPost(...) once and x increments by only 1 each time the browser is refreshed.
I tried Firefox 13.01 and IE 10.0.7. Both worked fine.

Then I upgraded Firefox, first to 19.02, and then to 23.01. It then did what you said: hit the doPost(..) twice.

Problem seems to be with Firefox sometime after 13.01.

Ed
Post Reply