?
Solved

How to debug AjaxRequest POST failure

Posted on 2016-09-06
3
Medium Priority
?
86 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-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

650 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