0

With reference of this question, this is how solved this without a function. Now I have a function and I try to call in javascript and also try to call that data in alert box but the alert box does not populate. My code is this:

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

function Loadchart() {
  $('#submitchart').click();
};
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

<form id="form1" runat="server">
  <asp:DropDownList ID="yearValue" runat="server" AutoPostBack="True" ViewStateMode="Enabled"></asp:DropDownList>
  <button id="submitchart" runat="server">Show Chart</button>
  <div id="container" style="width: 100%; height: 400px;"></div>
</form>

5
  • Is browser halting at debugger? Commented Jun 1, 2016 at 8:18
  • remove JSON.stringify Commented Jun 1, 2016 at 8:27
  • can you provide the format of the response? Commented Jun 1, 2016 at 8:28
  • i mean the json resulting from the ajax call Commented Jun 1, 2016 at 8:36
  • actually i am new in this java script and json .. Commented Jun 1, 2016 at 8:37

2 Answers 2

0
<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">
        <select id="yearValue"><option>1920</option></select>
        <button id="submitchart" runat="server" type="button">Show  Chart</button>
        <div id="container" style="width: 100%; height: 400px;"></div>
    </form>
</body>

<script type="text/javascript">
    $('#submitchart').click(function () {
        //alert("i");
        var webmethod = 'test.php';
        $.ajax({
            type: 'POST',
            url: webmethod ,
            data: JSON.stringify({ 
                yearP: $('#yearValue').val() 
            }),
            contentType: 'application/json;charset=utf-8',
            dataType: 'json',
            success: function(response) {
                alert('working');

            },
            error: function () {
                alert('error');
            }    
        });
    });

    function Loadchart() {
        $('#submitchart').click();
    };
</script>

Above code is working. Check and update with yours. Make sure button type button.

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

Comments

0

Wrap your code in a document ready statement,remove JSON.stringify

<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">
        <select id="yearValue"><option>1920</option></select>
        <button id="submitchart" runat="server" type="button">Show  Chart</button>
        <div id="container" style="width: 100%; height: 400px;"></div>
    </form>
</body>

<script type="text/javascript">
  $(function(){
    $('#submitchart').click(function (e) {
        e.preventDefault();
        var webmethod = 'test.php';
        $.ajax({
            type: 'POST',
            url: webmethod ,
            data: JSON.stringify({ 
                yearP: $('#yearValue').val() 
            }),
            contentType: 'application/json;charset=utf-8',
            dataType: 'json',
            success: function(response) {
                alert('working');

            },
            error: function () {
                alert('error');
            }    
        });
    }).trigger('click');
 });
</script>

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.