Solved

How to debug AjaxRequest POST failure

Posted on 2016-09-06
3
42 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to count occurrences of each item in an array.

758 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

18 Experts available now in Live!

Get 1:1 Help Now