Solved

How to debug AjaxRequest POST failure

Posted on 2016-09-06
3
51 Views
Last Modified: 2016-09-07
I am working with communities in a Salesforce org. I have a page that does a search for files using a commandLink with a param. Upon button click of the icon for the commandLink a controller method is called and returns the appropriate result based on the search string from the inputText. This all works correctly.

However, if I search using the exact same search string and hit the enter key, the POST fails and I do not get a response. I have a JS script that fires onKeyPress and checks for the enter key code. The script fires and gets to the point of calling the controller method and then  nothing...

here is the script:

            <script type="text/javascript">
                function handleKeyPress(e){
                    var key = 0;
                    if(window.event){
                        key = e.keyCode;
                    }else if(e.which){
                        key = e.which;
                    }
                    if(key == 13){
                        console.log("We are about to call to call the controller method");
                        findFileMethod();                                         
                    }
                }
            </script>

Open in new window


I'm debugging in Firebug trying to identify the issue. Here is the POST failure that I'm seeing in the console window.

screenshot
There doesn't appear to be any JS error and it never reaches the controller method, so I'm stuck on how to debug this and see what the issue is.

Any help is appreciated.
0
Comment
Question by:-Dman100-
  • 2
3 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 41786518
you have to kill the default behaviour for Enter key by returning the value false to the event handler result.
Like this:

... return handleKeyPress(event);

and your function does this:
            <script type="text/javascript">
                function handleKeyPress(e){
                    var key = 0;
                    if(window.event){
                        key = e.keyCode;
                    }else if(e.which){
                        key = e.which;
                    }
                    if(key == 13){
                        console.log("We are about to call to call the controller method");
                        findFileMethod();
                        return false;                                         
                    }
                    return true;
                }
            </script>

Open in new window

0
 

Author Closing Comment

by:-Dman100-
ID: 41786906
Thank you!! Yes, that worked. How did returning false fix the issue?
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 41787602
Your function handleKeyPress() works as an Event Handler and is able to do control over event preventDefault behaviour.
One of the ways to preventDefault is to return false from the Event Handler:
https://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-cancelation

In your case the Enter key initiated the Form submission and that stopped the Ajax flow.

Is this explanation enough for you?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why does my button disappear? 3 28
Show banners that are blocked by Ad Muncher 10 46
highcharts wont fix inside a bootstrap modal window 6 29
Remove lines by logo 2 22
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now