2

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

2
  • Have you checked in for example Chrome Developer tools Network tab whether the HTTP POST happens?
    – Bemmu
    Commented Sep 5, 2013 at 10:50
  • I see the 500 error in the network tab . Commented Sep 5, 2013 at 11:21

1 Answer 1

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.