0

I have tried many things but not getting response from this. I am trying to retrieve token from mydomin/token and it is responding well. I debugged that and its working fine but Ajax is not taking the response. I am using this

<script>
    //working but not getting response
    function myFunction1() {
       $.ajax({
            type: "GET",
            url: "/Account/CheckLogin?Email=" + $("#username").val()
            + "&password=" + $("#password").val(),
            success: function (data) {
                alert(data);
            }
        }).done(function (data) {
            alert(data);
        }).fail(showError);
    }
</script>

Here is my server code

 [HttpGet]
        public JsonResult CheckLogin(Account account)
        {
            Result obj = new Result();
            if (!account.Equals( null))
            {
                System.Diagnostics.Debug.Write(account.Email);
                System.Diagnostics.Debug.Write(account.Password);
                var result = db.Users.Any(x => (x.Username.Equals(account.Email) || x.Email.Equals(account.Email)) && x.Password.Equals(account.Password));
                if (result)
                {
                    //return RedirectToAction("Index", "Home");
                    obj.txt = true;
                    return Json(obj,JsonRequestBehavior.AllowGet);
                }
            }
            obj.txt = false;
            return Json(obj, JsonRequestBehavior.AllowGet);
        }

What i am getting response in browser and Post man is

{
txt: false
}

But ajax is not giving any response in data not showing alert. Even i did this to make sure but no alert not shown

 success: function (data) {
                alert("Hi");
            } 

I followed this and some other links on stack but didn't worked for me. how to call controller action in ajax url

Inshal Irshad
  • 227
  • 3
  • 17

2 Answers2

0

if you can't see alert message then the ajax call wasn't success, please try to debug it from the browser (chrome) hit f12 and check if you have any error and select network tab and then scroll through requests made to the server and locate the one made by your ajax call click on it and then you will find what is the problem you should be able to find your ajax call here in red if it was failing

  • This should be a comment and not an answer. – Cyclonecode Dec 24 '17 at 14:17
  • I have debugged my code its going fine in controller and controller is sending returning JSON. But it is not been received in Success method. – Inshal Irshad Dec 24 '17 at 14:19
  • @Cyclonecode i agree BUT i can't comment i don't have 50 reputations – Ahmad Albaz Dec 24 '17 at 14:22
  • GET http://localhost:59335/Account/assets/stylesheets/bootstrap.min.css net::ERR_ABORTED Login:17 GET http://localhost:59335/Account/assets/stylesheets/pixel-admin.min.css net::ERR_ABORTED Login:18 GET http://localhost:59335/Account/assets/stylesheets/widgets.min.css net::ERR_ABORTED Login:19 GET http://localhost:59335/Account/assets/stylesheets/rtl.min.css net::ERR_ABORTED – Inshal Irshad Dec 24 '17 at 14:27
  • This is what it is saying in browser @AhmadAlbaz – Inshal Irshad Dec 24 '17 at 14:27
  • @Inshalirshad yes please check the image attached, you have to debug it from the browser, one case i had where the data size were too much to be serialized as json and i had the same problem as yours everything goes fine in the controller and no exception occurred untill i debugged it from there – Ahmad Albaz Dec 24 '17 at 14:28
  • @Inshalirshad add this to your `.fail()` function `.fail(function( jqXHR, textStatus ) { alert( "Request failed: " + textStatus ); });` – Junius L Dec 24 '17 at 14:35
  • this is what it is saying i browser [link](https://ibb.co/iTOA5R) its giving error in css but it is not the matter as i looked into it because css is fine – Inshal Irshad Dec 24 '17 at 14:36
  • add the code I showed above, and it will show you the error. – Junius L Dec 24 '17 at 14:37
  • @julekgwa this is what it is showing 'Request failed: error' – Inshal Irshad Dec 24 '17 at 14:39
  • The problem is with your url or your back end, go do some debugging. – Junius L Dec 24 '17 at 14:42
  • check this answer https://stackoverflow.com/questions/7577275/jquery-ajax-requests-are-getting-cancelled-without-being-sent – Ahmad Albaz Dec 24 '17 at 14:49
0

My issue was I was trying to fire Ajax off the click event of a submit button that had a server side click event setup. I had to make the button just a simple button (i.e. )

Inshal Irshad
  • 227
  • 3
  • 17