gayathri
gayathri

Reputation: 3

Ajax request is not working

I am trying to get data from db using ajax. When a date is selected from inline calender then that date will be captured and query will be done and ive to show the holiday status of the selected date. but the ajax request is not working, how to know whether it is working or not.

my code is

$(function () {
    $("#divCalendar").datepicker({
        dateFormat: "mm-dd-yy",
        onSelect: function (selectedDate) {
            //alert("You clicked on " + selectedDate.toString());
            var dataString = 'sdt='+ selectedDate.toString();
            //alert("You clicked on " + dataString);
            $.ajax({
                type: "POST",
                url:"demotest.php",
                data: dataString,
                dataType : "json",
                success:function(data){
                    if(data != "ERROR")  
                    {
                        $("#div1").html(data);
                    }
                    else
                    {
                        $("#div1").html("nothing found");    
                    }
                }
            });
        }
    });
});    

Upvotes: 0

Views: 72

Answers (2)

SarathSprakash
SarathSprakash

Reputation: 4624

The dataType: attribute represents the type of data to be returned as response from server. But from you coding it seems like your response data is a simple string or text.so you can give like this dataType:"text," or you could just ignore datatType attribute, since by default it is text.

Try this ,it is working

$.ajax({
                type: "POST",
                url:"demotest.php",
                data: {
                       us:"hi"
                        },

               success:function(data){
                    if(data != "ERROR")  
                    {
                        $("#div1").html(data);
                    }
                    else
                    {
                        $("#div1").html("nothing found");    
                    }
                }
            });

Upvotes: 0

Mithun Satheesh
Mithun Satheesh

Reputation: 27845

in $.ajax, dataType parameter refers to the type of data that you're expecting back from the server as ajax response. You are setting that in your call to json.

But the line

$("#div1").html(data);

seems like you are expecting a normal text or html response. If the response is not valid json the ajax call will not work. Else you remove the dataType : "json", line and let the ajax utility look for a default text response.

You can get hints regarding the error encountered in your ajax call by checking the browser console (chrome inspector/firebug).

Upvotes: 1

Related Questions