sandeep.mishra
sandeep.mishra

Reputation: 825

jquery ajax call is not calling the server side method

I am calling a server side method by using jquery ajax post method.But it is not getting called Below is my code..

js

var templateName = $("#txtTemplateName").val().trim(),
htmlHeader = $("#txtHtmlHead").val().trim(),
header = $("#txtHeader").val().trim(),
footer = $("#txtFooter").val().trim()

var templateData = {
    templateName: templateName,
    htmlHeader: htmlHeader,
    header: header,
    footer: footer       
};

i created the javascript object

$.ajax({
    type: "POST",
    url: "template_brow.aspx/SaveTemplate",
    data: JSON.stringify(templateData),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(result) {
        alert(result.d);
        $("#divTemplate").find("input[type=text]").val('');
        $("#divTemplate").find("textarea").val('');
    },
    error: function() {
        alert("Error while calling the server!");
    }
});

and in server side i made a custom class with the above properties

[WebMethod(EnableSession = true)]
public static string SaveTemplate(TemplateVariables oTemplateVariables)
{
----

}

TemplateVariables is the custome class

Can anybody help me on this

Upvotes: 2

Views: 4167

Answers (1)

Rakesh Nayak
Rakesh Nayak

Reputation: 111

var templateName = $("#txtTemplateName").val().trim(),
htmlHeader = $("#txtHtmlHead").val().trim(),
header = $("#txtHeader").val().trim(),
footer = $("#txtFooter").val().trim()

var templateData = {
    templateName: templateName,
    htmlHeader: htmlHeader,
    header: header,
    footer: footer       
};

After this block of code write:

var strData={}; 
strData.oTemplateVariables = templateData;

And then in ajax call write "data: JSON.stringify(strData)" instead of "data: JSON.stringify(templateData)" as given below:

$.ajax({
    type: "POST",
    url: "template_brow.aspx/SaveTemplate",
    data: JSON.stringify(strData),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(result) {
        alert(result.d);
        $("#divTemplate").find("input[type=text]").val('');
        $("#divTemplate").find("textarea").val('');
    },
    error: function() {
        alert("Error while calling the server!");
    }
});

It will work.

Upvotes: 5

Related Questions