locked
Asp.Net Core Mvc JsonResult Not Triggered RRS feed

  • Question

  • User856008996 posted

    public class Quote
        {         
            public string FullName { get; set; }

            public string Email { get; set; }

            public string Phone { get; set; }

            public string Budget { get; set; }      

            public string Message { get; set; }
        }

    $("#submit").on('click', function () { $.ajax({ url: '/Home/QuoteMessageSend', type: "POST", dataType: 'json', data: $("#QuoteForm").serialize(), error: function (xhr, textStatus, error) { console.log(xhr.statusText); console.log(textStatus); console.log(error); } }); });

    <form id="QuoteForm">
                    <p>
                        <input id="fullName" asp-for="FullName" placeholder="Full Name" class="contact-item" />
                        <span id="fnoutput"></span>
                    </p>
                    <p>
                        <input id="email" asp-for="Email" placeholder="Email" class="contact-s-item" />
                        <span id="emoutput"></span>
                    </p>
                    <p>
                        <input id="phone" asp-for="Phone" placeholder="Phone" class="contact-s-item" />
                        <span id="phoneoutput"></span>
                    </p>
                    <p>
                        <select asp-for="Budget" class="contact-l-item">
                            <option value="" disabled selected>Budget</option>
                            <option value="200-1000">€200 - 800</option>
                            <option value="800-4000">€800 - €4000</option>
                            <option value="4000-12000">€4000 - 12000</option>
                            <option value="12000+">€12000+</option>
                        </select>
                        <span id="budgetoutput"></span>
                    </p>
                    <p>
                        <textarea id="message" asp-for="Message" placeholder="Your Ideas" class="contact-m-item"></textarea>
                        <span id="messageoutput"></span>
                    </p>
                    <button id="submit" type="button">Submit Now</button>
                </form>

              

    [HttpPost]
            public JsonResult QuoteMessageSend(Quote quote)
            {
                QformCheck(quote);
                if (qfncheck && qemcheck && qphonecheck && qbdcheck && qmsgcheck)
                {
                    using (MySqlConnection connection = new MySqlConnection("Server=localhost; Database=dbname; Uid=root; Pwd=123456; SSL Mode=none;"))
                    {
                        using (MySqlCommand cmd = new MySqlCommand("INSERT INTO quotes(FullName, Email, Phone, Budget, Message) VALUES(@fn, @em, @ph, @bdg, @msg)", connection))
                        {
                            connection.Open();
                            cmd.Parameters.AddWithValue("@fn", quote.FullName);
                            cmd.Parameters.AddWithValue("@em", quote.Email);
                            cmd.Parameters.AddWithValue("@ph", quote.Phone);
                            cmd.Parameters.AddWithValue("@bdg", quote.Budget);
                            cmd.Parameters.AddWithValue("@msg", quote.Message);
                            cmd.ExecuteNonQueryAsync();
                            connection.Close();
                        }
                    }
                    return Json(new { data = quote, success = true, responseText = "" });
                }
                else
                {
                    return Json(new { data = quote, success = true, responseText = "" });
                }                

            }

    While this code works flawlessly in local, it doesn't work when I broadcast. When I check the item and look at it, all the necessary code I can see it being called. I thought the problem might be in the database connection, I created a simple sample table and pulled data on the server works smoothly. However, when I register to the database with JsonResult, it does not get into the database almost on the server side. It does not give any errors. Just looking at the item with check item again, I could see that it wrote 404 bad request. Apart from that I have there is nothing.

    Monday, June 1, 2020 6:52 PM

All replies

  • User753101303 posted

    Hi,

    You mean you have a 404 not found for /Home/QuoteMessageSend ?

    Could it be that this web application is installed under a virtual foler ie at https://site.com/app/Home/QuoteMessageSend rather than https://site.com/Home/QuoteMessageSend ? 

    Also your method is not async so use ExecuteNonQuery() rather than ExecuteNonQueryAsync() for now.

    Monday, June 1, 2020 7:22 PM
  • User-474980206 posted

    if the following test:

                if (qfncheck && qemcheck && qphonecheck && qbdcheck && qmsgcheck)

    does not pass, nothing happens. you don't show where these defined or how initialed. 

    Monday, June 1, 2020 7:51 PM
  • User856008996 posted

    Your suggestions did not work. Would you suggest any other method than this method? My goal is to click the submit button without refreshing the page.

    Monday, June 1, 2020 9:06 PM
  • User-474980206 posted

    you don't say what you mean by not working.

    a quick look at your code, shows it makes both an ajax call, and does a form submit when the submit button is clicked. if the ajax is not happening, then there are probably errors in the browsers console. 

    Monday, June 1, 2020 9:10 PM
  • User856008996 posted

    I got the Bad Request 404 browser options. It does not seem to be any request on the Kestrel side. I'm getting this error probably because it can't reach QuoteMessageSend. I do not know why.

    I want to click the button without refreshing the page. We don't have to solve the problem this way.

    Monday, June 1, 2020 9:15 PM
  • User475983607 posted

    Your responses are very confusing.  A 404 is page not found.  A Bad request is a 400 error which means there is a problem with the client request.  anyway, I built a test and it works fine.

    @model MvcBasic.Models.Quote 
    @{
        ViewData["Title"] = "QuoteMessageSend";
    }
    
    <h1>QuoteMessageSend</h1>
    
    <form id="QuoteForm">
        <p>
            <input id="fullName" asp-for="FullName" placeholder="Full Name" class="contact-item" />
            <span id="fnoutput"></span>
        </p>
        <p>
            <input id="email" asp-for="Email" placeholder="Email" class="contact-s-item" />
            <span id="emoutput"></span>
        </p>
        <p>
            <input id="phone" asp-for="Phone" placeholder="Phone" class="contact-s-item" />
            <span id="phoneoutput"></span>
        </p>
        <p>
            <select asp-for="Budget" class="contact-l-item">
                <option value="" disabled selected>Budget</option>
                <option value="200-1000">€200 - €800</option>
                <option value="800-4000">€800 - €4000</option>
                <option value="4000-12000">€4000 - €12000</option>
                <option value="12000+">€12000+</option>
            </select>
            <span id="budgetoutput"></span>
        </p>
        <p>
            <textarea id="message" asp-for="Message" placeholder="Your Ideas" class="contact-m-item"></textarea>
            <span id="messageoutput"></span>
        </p>
        <button id="submit" type="button">Submit Now</button>
    </form>
    
    @section scripts {
    
    <script>
        $("#submit").on('click',
            function (e) {
                e.preventDefault();
                $.ajax({
                    url: '@Url.Action("QuoteMessageSend", "Home")',
                    type: "POST",
                    dataType: 'json',
                    data: $("#QuoteForm").serialize(),
                    success: function (response) {
                        console.log(response);
                    },
                    error: function (xhr, textStatus, error) {
                        console.log(xhr.statusText);
                        console.log(textStatus);
                        console.log(error);
                    }
                });
            });
    
    </script>
    
    }
    
    
            [HttpGet]
            public ActionResult QuoteMessageSend()
            {
                return View();
            }
    
            [HttpPost]
            public JsonResult QuoteMessageSend(Quote quote)
            {
                return Json(quote);
            }

    Monday, June 1, 2020 10:21 PM
  • User856008996 posted

    Your responses are very confusing.  A 404 is page not found.  A Bad request is a 400 error which means there is a problem with the client request.  anyway, I built a test and it works fine.

    @model MvcBasic.Models.Quote 
    @{
        ViewData["Title"] = "QuoteMessageSend";
    }
    
    <h1>QuoteMessageSend</h1>
    
    <form id="QuoteForm">
        <p>
            <input id="fullName" asp-for="FullName" placeholder="Full Name" class="contact-item" />
            <span id="fnoutput"></span>
        </p>
        <p>
            <input id="email" asp-for="Email" placeholder="Email" class="contact-s-item" />
            <span id="emoutput"></span>
        </p>
        <p>
            <input id="phone" asp-for="Phone" placeholder="Phone" class="contact-s-item" />
            <span id="phoneoutput"></span>
        </p>
        <p>
            <select asp-for="Budget" class="contact-l-item">
                <option value="" disabled selected>Budget</option>
                <option value="200-1000">€200 - €800</option>
                <option value="800-4000">€800 - €4000</option>
                <option value="4000-12000">€4000 - €12000</option>
                <option value="12000+">€12000+</option>
            </select>
            <span id="budgetoutput"></span>
        </p>
        <p>
            <textarea id="message" asp-for="Message" placeholder="Your Ideas" class="contact-m-item"></textarea>
            <span id="messageoutput"></span>
        </p>
        <button id="submit" type="button">Submit Now</button>
    </form>
    
    @section scripts {
    
    <script>
        $("#submit").on('click',
            function (e) {
                e.preventDefault();
                $.ajax({
                    url: '@Url.Action("QuoteMessageSend", "Home")',
                    type: "POST",
                    dataType: 'json',
                    data: $("#QuoteForm").serialize(),
                    success: function (response) {
                        console.log(response);
                    },
                    error: function (xhr, textStatus, error) {
                        console.log(xhr.statusText);
                        console.log(textStatus);
                        console.log(error);
                    }
                });
            });
    
    </script>
    
    }
    
            [HttpGet]
            public ActionResult QuoteMessageSend()
            {
                return View();
            }
    
            [HttpPost]
            public JsonResult QuoteMessageSend(Quote quote)
            {
                return Json(quote);
            }

    I also published the application in the same way. The process of inserting into the database runs smoothly on localhost. But it doesn't work when I broadcast on VPS with Nginx & Kestrel. I don't know what's wrong. The only error I got is that QuoteMessageSend cannot be found.

    Tuesday, June 2, 2020 1:20 AM