user6408005 user6408005 - 6 months ago 15
jQuery Question

data in alert box through function javascript

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:

<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">
<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>
</body>

<script type="text/javascript">
$('#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>


Any solution?

Answer
<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.

Comments