0

I try to populate data in alert box on button click but when I click on button then alert box not populate

I try this

<head runat="server">
  <title></title>
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

</head>

<body>
  <form id="form1" runat="server">
    <div>
      <button id="submitchart" runat="server">Show Chart</button>
    </div>
  </form>
</body>
<script type="text/javascript">
  $('#submitchart').click(function() {
    //alert("i");        
    $.ajax({
      type: 'POST',
      //url: 'WebForm1.aspx / Jqufunc',
      //data: JSON.stringify({ yearP: $('#yearValue').val() }),
      //data:{},
      contentType: 'application/json;charset=utf-8',
      dataType: 'json',
      success: function() {
        //alert(JSON.stringify(response.d));
        alert("i");
      }

    });
  });
  //});
</script>

when I write alert("i") after this line $('#submitchart').click(function () { then alert box populate but when i write alert ("i") after the success: function() { then alert box not populate

any solution?

5
  • 1
    that means the ajax is not a success add error function so you will. check the dev tools in XHR for ajax response..check console for error Commented Jun 1, 2016 at 6:42
  • 1
    why you have not set url in ajax??? Commented Jun 1, 2016 at 6:43
  • any console errors? Commented Jun 1, 2016 at 6:56
  • how to check console errors? Commented Jun 1, 2016 at 7:10
  • check my updated question please Commented Jun 1, 2016 at 8:01

3 Answers 3

1

  $(function () {
            $("#submitchart").on("click", function () {
                debugger;
                alert("i");
                $.ajax({
                    type: 'POST',
                    //url: 'WebForm1.aspx / Jqufunc',
                    //data: JSON.stringify({ yearP: $('#yearValue').val() }),
                    //data:{},
                    contentType: 'application/json;charset=utf-8',
                    dataType: 'json',
                    success: function () {
                        //alert(JSON.stringify(response.d));
                        debugger;
                        alert("i");
                    },
                    error: function () {
                        debugger;
                        alert("i");
                    }

                });

ajax will be run error,don't run success

Sign up to request clarification or add additional context in comments.

1 Comment

why we write debugger?
0

Give button type as button.

<head runat="server">
      <title></title>
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

    </head>

    <body>
      <form id="form1" runat="server">
        <div>
          <button id="submitchart" type="button" runat="server">Show Chart</button>
        </div>
      </form>
    </body>
    <script type="text/javascript">
      $('#submitchart').click(function() {
        //alert("i");        
        $.ajax({
          type: 'POST',
          url: 'WebForm1.aspx / Jqufunc',
          data: JSON.stringify({ yearP: $('#yearValue').val() }),
          //data:{},
          contentType: 'application/json;charset=utf-8',
          dataType: 'json',
          success: function() {
            //alert(JSON.stringify(response.d));
            alert("i");
          }

        });
      });
      //});
    </script>

This one worked for me.

3 Comments

check this my another question .. stackoverflow.com/questions/37563080/…
i use function but still alert box not display
the problem is button type. by default button will work as submit button. if you click submit what ll happen. it will reload to given url. if url is not there then reload with in same page.
0

This happening because when you write alert("i") after $('#submitchart').click(function () { then its not dependent on success of any ajax call and ot works fine as it should but when you write this after success: function() { then it is dependent on ajax call and if there is some errors in ajax it will not run as you have only defined it for successful ajax.

Write this if ajax contains errors also

$('#submitchart').click(function() {
    //alert("i");        
    $.ajax({
      type: 'POST',
      //url: 'WebForm1.aspx / Jqufunc',
      //data: JSON.stringify({ yearP: $('#yearValue').val() }),
      //data:{},
      contentType: 'application/json;charset=utf-8',
      dataType: 'json',
      success: function() {
        //alert(JSON.stringify(response.d));
        alert("i");
      },
      error: function() {
        //alert(JSON.stringify(response.d));
        alert("i");
      }

    });
  });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.