0

I am trying to send data from my javascript to an aspx webmethod with ajax. the Success option of ajax post is fired but my web method never getting called. the javascript code is:

 $.ajax({
    type: "POST",
    url: '../About.aspx/GetWFData',
    data: "{sendData: '" + 5 + "'}",
    async: true,
    success: function (result) {
        alert("Bravo");
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert(textStatus);
        alert(errorThrown);
    }
});

and the webmethod in the code behind is:

 [WebMethod]
    public static string GetWFData(string sendData)
    {
        return String.Format("Hello");
    }
10
  • your json isn't valid. does ASP require it to be? you also forgot to set the contentType. Commented Jul 16, 2015 at 17:05
  • When i set the content type attribute i get this error "unauthorised 401" could you give an example of valid json Commented Jul 16, 2015 at 17:06
  • {"foo":"bar"} would be valid. All keys must be wrapped in double quotes, and strings must be wrapped in double quotes. It's better to not manually create JSON, instead, use the built-in method. JSON.stringify({foo: 'bar'}) will return {"foo":"bar"}(as a string) Commented Jul 16, 2015 at 17:06
  • i changed the[ data : JSON.stringify({foo: 'bar'}),] but still same issue?? Commented Jul 16, 2015 at 17:13
  • did you add teh contentType? Commented Jul 16, 2015 at 17:23

1 Answer 1

0

Try to use below code to see if it works.

var params = '{sendData:' + values + '}';
 $.ajax({
    type: "POST",
    url: '../About.aspx/GetWFData',
    data: JSON.stringify(params),
    async: true,
    success: function (result) {
        alert("Bravo");
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert(textStatus);
        alert(errorThrown);
    }
});

And refer to http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/ for more info. Try to disable request validation on a page.http://www.asp.net/whitepapers/request-validation

Sign up to request clarification or add additional context in comments.

2 Comments

the status code is Ok. how to get the return value of my webmethod in the ajax sucess method to check if the web method is fired or not?
From the chrome developper tool it seems that the response i was getting from calling the web method is the whole page!!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.