954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Javascript shows an error when no error was returned

I have written a javascript function that calls a web service to save data. When the web service is called an alert pops up saying the server method saveUpdateCodes failed, and it gives no other information. The thing is the web service executes and throws no errors (I've stepped though it). Also, the alerts in the catch in the javascript code never happen. Does anyone no how this can happen? Could it be the browser, my code...? For testing, I am using Firefox w/ the firebug plugin. The javascript code is below, if you need more information, please ask. Thanks.

This is the call to the web servcie

try
    {
        //debugger;
        //call the service to do the save
        codeList.editcodes.saveUpdateCodes(sUsrList, sCharge, sCodes, sId, afterSave, OnsaveUpdateCodesFailure);
        
        //close the progress panel
        hideProgress(); 
    
    }
    catch(e)
    {
        //debugger;
        alert(e.description);
        alert(e.number);
    }


These are my onSuccess & onFailure functions

function afterSave(result)
{
    //the web service returns an empty string on success & a err message onFailure
    //if the save was successfull, go to the previous page
    if (result == '')
        history.go(-1); 
    else
    {
        alert('There was an error saving the data. Please try again');
        //may need to go back here
    }
    hideProgress();   
}

function OnsaveUpdateCodesFailure( result )
{
    alert( result.get_message() );
    //history.go(-1);
    //debugger;
}
emurf59
Newbie Poster
4 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

yes , its still not clear from the post that what exactly your script is trying to achieve.explain that too and this is not the complete code i guess..

network18
Practically a Master Poster
619 posts since Sep 2009
Reputation Points: 29
Solved Threads: 76
 
yes , its still not clear from the post that what exactly your script is trying to achieve.explain that too and this is not the complete code i guess..

Sorry for any confusion, I'll try to be a little more clear and I've also learned a few things about what is happening. The code that you see is simply calling a web service and passing params that are saved to a database by the webservice. You are right, it isn't all the code. The rest of the code just gets data from the form and stores in those vars that are being passed.

What I have learned studying the situation is that javascript is not waiting for a response from the web service. It continues on and determines that result is undefined. While that is happening the webservice goes ahead and successfully saves the data then returns success. The problem is, javascript had already moved on as if what was returned was undefined. (I hope I'm saying this in a way that makes sense).

I guess now my question is, how do I make javascript wait for an answer from the webservice? Any ideas? Do you think that I need to post a new question?

Thanks.

emurf59
Newbie Poster
4 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

Where is your implementation for saveUpdateCodes? My guess is that your bug is in that function.

hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 244
 

Hi Hielo, thanks for the interest and thanks to everyone that helped. I found a workaround. The problem wasn't the web service but that javascript wasn't waiting for a response from the web service. What I did to fix this was to set the return type of the web service to void instead of string and now it runs successfully every time.

I think that having success or error returned from the web service would be the best coding practice but this will let me proceed with the project and get back on schedule.

Thanks,

emurf59
Newbie Poster
4 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 
that javascript wasn't waiting for a response from the web service


I know, that's why I wanted to see your js implementation of saveUpdateCodes(). My guess is you are sending an asynchronous request be do not have a callback function to determine the completion of the request.

Alternatively you can execute a synchronous request.

hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 244
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: