iJade
iJade

Reputation: 23791

jquery+AJAX+c# causing page load

i'm trying to make an Ajax call but its causing page load Here is my jquery code

    var ajaxUrl = "AjaxCallHandler.aspx";
function _init_Chart() {

$.ajax({
    type: "GET",        //GET or POST or PUT or DELETE verb
    url: ajaxUrl,       // Location of the service
    data: "OpCode=GetCallAverageReportForGraph&Parms=DeptId^17~Month^10~Year^2012",         //Data sent to server
    contentType: "",    // content type sent to server
    dataType: "string",     //Expected data format from server
    processdata: true,  //True or False
    success: function (responseString) {//On Successful service call
        alert(responseString);
    }
});

return false;
}

And here is my AjaxCallHandler.aspx.cs c# code

protected void Page_Load(object sender, EventArgs e)
{
    string responseMessage = "";
    string status = "SUCCESS";
    try
    {
        if (Request.QueryString["OpCode"] == null)
        {
            throw new Exception("Invalid Request, OpCode missing.");
        }
        string operationRequested = Request.QueryString["OpCode"];
        string Params = Request.QueryString["Parms"];
        switch (operationRequested)
        {
            case "GetCallAverageReportForGraph":
                responseMessage = GetCallAverageReportForGraph(Params);
                break;
            case "GetCallAverageReportDetails":
                responseMessage = GetCallAverageReportDetails(Params);
                break;
        }
    }
    catch (Exception exp)
    {
        status = "EXCEPTION";
        responseMessage = exp.Message;
    }
    Response.ClearContent();
    Response.ClearHeaders();
    Response.Write(responseMessage);
}

I dont know why this thing is causing a page load.i'm new to jquery AJAX but when i tried this with javascript AJAX it was working fine without any page load.

Upvotes: 3

Views: 1857

Answers (1)

Jeremy Morehouse
Jeremy Morehouse

Reputation: 801

I'm assuming that is the code behind for your AjaxCallHandler.aspx?

If so, your AJAX call is essentially just loading the results of that page just like you would if you were hitting it with a browser window (i.e. a GET).

Most of the time when you want to do an AJAX request, you would be calling it against an MVC function that returns a JSONResult ActionResult or a Web Service (.asmx) for example.

Since you're just hitting a regular web page, it will always do a Page_Load event on that page's code behind.

Upvotes: 2

Related Questions