Answered by:
AJAX: 'return' statement outside of function

Question
-
User-2062901272 posted
Hi Expert,
I have a problem on why this error always pop-out,
Where i could gone wrong. Kindly see my code.
<script type="text/javascript"> function ShowModalPopupPassword() { var EmailID = document.getElementById('<%=txtVerifyEmail.ClientID%>'); var emailAddress = EmailID.value; var parameter = { EmailAddress: 'myemail@gmail.com' }; $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "VerifyClientEmail.aspx/CheckReservEmail", data: JSON.stringify(parameter), dataType: "json", success: function (f) { alert('x'); }, error: function (result) { console.log(result.status) alert("Error " + result.status); } }); } </script>
Best Regards.
Monday, January 8, 2018 9:10 AM
Answers
-
User753101303 posted
But what happens ?
You should never try to debug your code by just reading it. Instead you should start from what happens (seems you do show an http status) and use F12 and debugger tools so that you can then focus on what matters...
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 9, 2018 8:11 AM
All replies
-
User753101303 posted
Hi,
And the status is ? You should always start from the error and then only see the relevent code portion rather than starting straigt away by reading the code without paying attention at which exact error you have.
For example 404 would point at a wrong location for your service, 500 at a server side error etc... (the mail address is hardcoded but it's likely not the problem for now)
Monday, January 8, 2018 10:31 AM -
User632428103 posted
Hello all,
@ryoka012 => check your parameter in your page method i think it would be the same (capital letter must be respect)
EmailAddress
Monday, January 8, 2018 11:22 AM -
User475983607 posted
It is not possible to deduce the issue from the code snippet.
Developer tools will allow you to see the error returned. Also I would change the alert functions to console.log() then you can copy and paste the error here.
Otherwise post the web method and any complex method parameter.
Monday, January 8, 2018 12:30 PM -
User-2062901272 posted
Hi,
Thank you for the reply.
Here is the complete snipnet.
<asp:Button ID="btnLogin" runat="server" OnClientClick="return ShowModalPopupPassword();" Font-Size="13px" Height="35px" Text="Verify Email" Width="100px" /> <asp:LinkButton ID="lnkDummy" runat="server"></asp:LinkButton> <asp:ModalPopupExtender ID="ModalPopupExtender1" BehaviorID="mpe" runat="server" PopupControlID="pnlPopup" TargetControlID="lnkDummy" BackgroundCssClass="modalBackground"> </asp:ModalPopupExtender> <asp:Panel ID="pnlPopup" runat="server" CssClass="modalPopup" Style="display: none" HorizontalAlign="Center"> <div class="body" style="width: 500px"> <asp:Image ID="Image1" runat="server" BorderColor="#666666" BorderWidth="1px" with="300px" Height="300px" /> <br /> <asp:Button ID="Button1" runat="server" Text="Close Visa Image" OnClientClick="return HideModalPopup()" /> </div> </asp:Panel> <script type="text/javascript"> function ShowModalPopupPassword() { var EmailID = document.getElementById('<%=txtVerifyEmail.ClientID%>'); //var emailAddress = EmailID.value; var parameter = { EmailAddress: 'jaquino012@gmail.com' }; $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "VerifyClientEmail.aspx/CheckReservEmail", data: JSON.stringify(parameter), dataType: "json", success: function (f) { alert('x'); }, error: function (result) { console.log(result.status) alert("Error " + result.status); } }); } </script>
Here is my code behind.
<WebMethod()> _ Public Shared Function CheckReservEmail(ByVal EmailAddress As String) As List(Of LoginDetail) Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) Dim LoginDetails As New List(Of LoginDetail)() Dim VBCommonCmd As New VBCommon Try con.Open() Dim cmd As SqlCommand = con.CreateCommand cmd.CommandType = CommandType.StoredProcedure cmd.CommandText = "usp_checkReservedEmailAddress" cmd.Parameters.AddWithValue("@EmailAddress", Trim(EmailAddress)) Dim dr As SqlDataReader = cmd.ExecuteReader Dim dt As New DataTable dt.Load(dr) LoginDetails.Add(New LoginDetail() With {.ExistItem = dt.Rows(0).Item("getValue").ToString(), .LocationItem = dt.Rows(0).Item("GCPLocation").ToString()}) Return LoginDetails cmd.ExecuteNonQuery() Catch ex As Exception HttpContext.Current.Session("error_message") = ex.Message & "</br>" & "Please check Email Verification Data in JSON." VBCommonCmd.Redirect("~/Errorpage.aspx") Return LoginDetails Finally If con IsNot Nothing Then con.Close() con.Dispose() End If End Try End Function Public Class LoginDetail Public Property ExistItem() As String Public Property LocationItem() As String End Class
Best Regards
Monday, January 8, 2018 11:46 PM -
User632428103 posted
Hello all,
@ryoka012 => i don't know very well vb net but for me your code is ok
just one thing, are you sure you add the reference to the jquery ?
read this complete article with a little sample perhpas it can help you
https://www.aspsnippets.com/Articles/Call-ASPNet-Page-Method-using-jQuery-AJAX-Example.aspx
Tuesday, January 9, 2018 7:57 AM -
User753101303 posted
But what happens ?
You should never try to debug your code by just reading it. Instead you should start from what happens (seems you do show an http status) and use F12 and debugger tools so that you can then focus on what matters...
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, January 9, 2018 8:11 AM -
User-2062901272 posted
Hi,
Thanks for the reply.
Yeah i just figured it out the i have two jquery reference that overlap and a code behind that gives me an error.
It is my error for not debugging correctly my code.
Again thank for your response.
Tuesday, January 9, 2018 8:13 AM