SUPER_USER
SUPER_USER

Reputation: 273

data in alert box using javascript

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?

Upvotes: 0

Views: 1712

Answers (3)

Gaurav Aggarwal
Gaurav Aggarwal

Reputation: 10187

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");
      }

    });
  });

Upvotes: 0

Kaja Mydeen
Kaja Mydeen

Reputation: 585

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.

Upvotes: 0

zhangyx007
zhangyx007

Reputation: 35

  $(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

Upvotes: 1

Related Questions