locked
Jquery 500 internal error RRS feed

  • Question

  • User1941728045 posted

    Im getting the above error, when im returning back from my Controller after clicking on "sub_btn". The return to the View() is going to the "error" section of the jquery:

    JQuery is below for ALLTLHeaderTR,cshtml. The part executed would be for "sub_btn"

    script type="text/javascript">
          $(function () {
             $('#dTable').DataTable();
    
             $('#dTable tbody').on('click', 'tr', function () {
                var tID = $.trim($(this).find("td").eq(0).text());
                $(this).toggleClass('clickable');
    
                var self = this;
                $.ajax(
                         {
                            type: "POST",
                            url: "/TL/AllTDetail",
                            data: { tlID: tID },
                            dataType: 'text',
                            success: function (data) {
                               $('html, body').animate({ scrollTop: 0 }, 'slow');
                               $('#result').html(data);
                               $(self).off('click');
                            }
                         });
             });
    
             $(document).ready(function () {
                var oTable = $(".dTable").dataTable({
                   "sPaginationType": "full_numbers",
                   "iDisplayLength": 10,
                   "bDestroy": true,
                   "aLengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]]
                }).after('<br />');
    
                $("#sub_btn").click(function () {
                   $.ajax({
                      type: "POST",
                      url: "/Home/Subscribe",
                      data: { subscribe_email: $("#subEmail").val() },
                      datatype: "text",
                      success: function (response) {
                         alert("You have successfully subscribed");
                         $('#sub_form')[0].reset();
                      },
                      error: function () {
                         debugger;
                         alert("Error in adding your email address. Please notify support to rectify the issue.");
                         $('#sub_form')[0].reset();
                      }
                   });
                });
    
             });
          });
    </script>

    Here is the button part within ALLTLHeaderTR,cshtml

                         <form id="sub_form">
                            <div class="input-group">
                               <input id="subEmail" type="text" class="form-control sub-box" placeholder="Your Email Address	" />
                               <span class="input-group-btn"
                                     style="width:10%;">
                                  <button id="sub_btn" class="btn btn-default sub-btn" type="button">Subscribe</button>
                               </span>
                            </div>
                            <!-- /input-group -->
                         </form>

    And here is the POST of Controller which inserts the email address into the DB before returning back to ALLTLHeaderTR.cshtml

    [HttpPost]
            public ActionResult Subscribe(string subscribe_email)
            {
               DBController dbcontroller = new DBController();
    
               if (dbcontroller.DBConnection())
               {
                  MySqlCommand command = new MySqlCommand("insert_subscription", dbcontroller.conn);
                  command.CommandType = System.Data.CommandType.StoredProcedure;
    
                  command.Parameters.Add(new MySqlParameter("subscribe_email", subscribe_email));
                  command.Parameters["@subscribe_email"].Direction = System.Data.ParameterDirection.Input;
    
                  try
                  {
                     command.ExecuteNonQuery();
    
                     dbcontroller.conn.Close();
                  }
    
                  catch (MySql.Data.MySqlClient.MySqlException ex)
                  {
                     dbcontroller.conn.Close();
                     ViewBag.Message = "Could not add you to the the database. Error " + ex.Number + " has ocurred. Please try again or contact the system administrator";
    
                     return View();
                  }
    
                  return View();
               }
               else
               {
                  ViewBag.Message = "Could not connect to the database. Please try again or contact the system administrator";
                  return View();
               }
            }

    My database is being updated when going into the Controller, and the return View(), is being executed correctly. Im not sure why its going to the "error" on Jquery.

    I did do a F12, and have been trying to debug using Chrome, but very unsuccessful.

    Please can you assoit me .. 

    Thanks.,

    Naren

    Sunday, October 13, 2019 10:20 PM

Answers

  • User283571144 posted

    Hi ngokal,

    According to your description and codes, I guess the reason why you get the 500 error is you may not pass the right view to the client-side.

    I found you could directly call return view in the Subscribe post method, if the view folder doesn't contain the Subscribe view, it will throw the 500 error.

    I suggest you could pass the view name which you want to return to the client-side into the view() method. Then it will be work well.

    Details, you could refer to below codes:

       $(document).ready(function () {
                       
                        $("#sub_btn").click(function () {
                            $.ajax({
                                type: "POST",
                                url: "/Default/Subscribe",
                                data: { subscribe_email: $("#subEmail").val() },
                                datatype: "text",
                                success: function (response) {
                                    alert("You have successfully subscribed");
                                    $('#sub_form')[0].reset();
                                },
                                error: function () {
                                    debugger;
                                    alert("Error in adding your email address. Please notify support to rectify the issue.");
                                    $('#sub_form')[0].reset();
                                }
                            });
                        });
    
    
    //Controller:
     public ActionResult Subscribe(string subscribe_email)
            {
               return View("Index");
            }
    

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 14, 2019 6:39 AM